comparison semiconginev2/build.nim @ 1218:56781cc0fc7c compiletime-tests

did: renamge main package
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 21:01:37 +0700
parents semicongine/build.nim@a3eb305bcac2
children
comparison
equal deleted inserted replaced
1217:f819a874058f 1218:56781cc0fc7c
1 # this should be used with nimscript
2
3 import std/strformat
4 import std/os
5 import std/strutils
6
7 include ./core/globals
8
9 const BLENDER_CONVERT_SCRIPT = currentSourcePath().parentDir().parentDir().joinPath("tools/blender_gltf_converter.py")
10 const STEAMCMD_ZIP = currentSourcePath().parentDir().parentDir().joinPath("tools/steamcmd.zip")
11 const STEAMBUILD_DIR_NAME = "steam"
12
13 var STEAMLIB: string
14 if defined(linux):
15 STEAMLIB = currentSourcePath().parentDir().parentDir().joinPath("libs/libsteam_api.so")
16 elif defined(windows):
17 STEAMLIB = currentSourcePath().parentDir().parentDir().joinPath("libs/steam_api.dll")
18 else:
19 raise newException(Exception, "Unsupported platform")
20
21 proc semicongine_builddir*(buildname: string, builddir = "./build"): string =
22 assert projectName() != "", "Please specify project file as a commandline argument"
23 var platformDir = "unkown"
24
25 if defined(linux):
26 platformDir = "linux"
27 elif defined(windows):
28 platformDir = "windows"
29 else:
30 raise newException(Exception, "Unsupported platform")
31
32 return builddir / buildname / platformDir / projectName()
33
34 proc semicongine_build_switches*(buildname: string, builddir = "./build") =
35 switch("experimental", "strictEffects")
36 switch("experimental", "strictFuncs")
37 switch("define", "nimPreviewHashRef")
38 if defined(linux):
39 switch("define", "VK_USE_PLATFORM_XLIB_KHR")
40 elif defined(windows):
41 switch("define", "VK_USE_PLATFORM_WIN32_KHR")
42 switch("app", "gui")
43 else:
44 raise newException(Exception, "Unsupported platform")
45
46 switch("outdir", semicongine_builddir(buildname, builddir = builddir))
47 switch("passL", "-Wl,-rpath,'$ORIGIN'") # adds directory of executable to dynlib search path
48
49 proc semicongine_pack*(outdir: string, bundleType: string, resourceRoot: string, withSteam: bool) =
50 switch("define", "PACKAGETYPE=" & bundleType)
51
52 assert resourceRoot.dirExists, &"Resource root '{resourceRoot}' does not exists"
53
54 outdir.rmDir()
55 outdir.mkDir()
56
57 echo "BUILD: Packing assets from '" & resourceRoot & "' into directory '" & outdir & "'"
58 let outdir_resources = joinPath(outdir, RESOURCEROOT)
59 if bundleType == "dir":
60 cpDir(resourceRoot, outdir_resources)
61 elif bundleType == "zip":
62 outdir_resources.mkDir()
63 for resourceDir in resourceRoot.listDirs():
64 let outputfile = joinPath(outdir_resources, resourceDir.splitPath().tail & ".zip")
65 withdir resourceDir:
66 if defined(linux):
67 echo &"zip -r {relativePath(outputfile, resourceDir)} ."
68 exec &"zip -r {relativePath(outputfile, resourceDir)} ."
69 elif defined(windows):
70 echo &"powershell Compress-Archive * {relativePath(outputfile, resourceDir)}"
71 exec &"powershell Compress-Archive * {relativePath(outputfile, resourceDir)}"
72 else:
73 raise newException(Exception, "Unsupported platform")
74 elif bundleType == "exe":
75 switch("define", "BUILD_RESOURCEROOT=" & joinPath(getCurrentDir(), resourceRoot)) # required for in-exe packing of resources, must be absolute
76 if withSteam:
77 STEAMLIB.cpFile(outdir.joinPath(STEAMLIB.extractFilename))
78
79 proc semicongine_zip*(dir: string) =
80 withdir dir.parentDir:
81 let zipFile = dir.lastPathPart & ".zip"
82 if zipFile.fileExists:
83 zipFile.rmFile()
84 if defined(linux):
85 exec &"zip -r {dir.lastPathPart} {dir.lastPathPart}"
86 elif defined(windows):
87 exec &"powershell Compress-Archive * {dir.lastPathPart}"
88 else:
89 raise newException(Exception, "Unsupported platform")
90
91
92 # need this because fileNewer from std/os does not work in Nim VM
93 proc fileNewerStatic(file1, file2: string): bool =
94 assert file1.fileExists
95 assert file2.fileExists
96 when defined(linux):
97 let command = "/usr/bin/test " & file1 & " -nt " & file2
98 let ex = gorgeEx(command)
99 return ex.exitCode == 0
100 elif defined(window):
101 {.error "Resource imports not supported on windows for now".}
102
103 proc import_meshes*(files: seq[(string, string)]) =
104 if files.len == 0:
105 return
106
107 var args = @["--background", "--python", BLENDER_CONVERT_SCRIPT, "--"]
108 for (input, output) in files:
109 args.add input
110 args.add output
111
112 exec("blender " & args.join(" "))
113
114 proc import_audio*(files: seq[(string, string)]) =
115 for (input, output) in files:
116 let command = "ffmpeg " & ["-y", "-i", input, "-ar", $AUDIO_SAMPLE_RATE, output].join(" ")
117 exec(command)
118
119 proc semicongine_import_resource_file*(resourceMap: openArray[(string, string)]) =
120 when not defined(linux):
121 {.warning: "Resource files can only be imported on linux, please make sure that the required files are created by runing the build on a linux machine.".}
122 return
123 var meshfiles: seq[(string, string)]
124 var audiofiles: seq[(string, string)]
125
126 for (target_rel, source_rel) in resourceMap:
127 let target = thisDir().joinPath(target_rel)
128 let source = thisDir().joinPath(source_rel)
129 if not source.fileExists:
130 raise newException(IOError, &"Not found: {source}")
131 if not target.fileExists or source.fileNewerStatic(target):
132 echo &"{target} is outdated"
133 if source.endsWith("blend"):
134 meshfiles.add (source, target)
135 elif source.endsWith("mp3") or source.endsWith("ogg") or source.endsWith("wav"):
136 audiofiles.add (source, target)
137 else:
138 raise newException(Exception, &"unkown file type: {source}")
139 target.parentDir().mkDir()
140 else:
141 echo &"{target} is up-to-date"
142 import_meshes meshfiles
143 import_audio audiofiles
144
145
146 # for steam-buildscript docs see https://partner.steamgames.com/doc/sdk/uploading
147 proc semicongine_steam_upload*(steamaccount, password, buildscript: string) =
148 let steamdir = thisDir().joinPath(STEAMBUILD_DIR_NAME)
149 if not dirExists(steamdir):
150 steamdir.mkDir
151 let zipFilename = STEAMCMD_ZIP.extractFilename
152 STEAMCMD_ZIP.cpFile(steamdir.joinPath(zipFilename))
153 withDir(steamdir):
154 if defined(linux):
155 exec &"unzip {zipFilename}"
156 rmFile zipFilename
157 exec "steamcmd/steamcmd.sh +quit" # self-update steamcmd
158 elif defined(windows):
159 exec &"powershell Expand-Archive -LiteralPath {zipFilename} ."
160 rmFile zipFilename
161 exec "steamcmd/steamcmd.exe +quit" # self-update steamcmd
162 else:
163 raise newException(Exception, "Unsupported platform")
164
165 var steamcmd: string
166 if defined(linux):
167 steamcmd = STEAMBUILD_DIR_NAME.joinPath("steamcmd").joinPath("steamcmd.sh")
168 elif defined(windows):
169 steamcmd = STEAMBUILD_DIR_NAME.joinPath("steamcmd").joinPath("steamcmd.exe")
170 else:
171 raise newException(Exception, "Unsupported platform")
172 let scriptPath = "..".joinPath("..").joinPath(buildscript)
173 exec &"./{steamcmd} +login \"{steamaccount}\" \"{password}\" +run_app_build {scriptPath} +quit"
174
175 proc semicongine_sign_executable*(file: string) =
176 const SIGNTOOL_EXE = "C:/Program Files (x86)/Windows Kits/10/App Certification Kit/signtool.exe"
177 if not SIGNTOOL_EXE.fileExists:
178 raise newException(Exception, &"signtool.exe not found at ({SIGNTOOL_EXE}), please install the Windows SDK")
179 exec &"\"{SIGNTOOL_EXE}\" sign /a /tr http://timestamp.globalsign.com/tsa/r6advanced1 /fd SHA256 /td SHA256 {file}"