Mercurial > games > semicongine
annotate semicongine/build.nim @ 1127:073ce95ae5c7
fix: wrong string interpolation
author | sam <sam@basx.dev> |
---|---|
date | Mon, 29 Apr 2024 02:37:42 -0700 |
parents | 881be2633761 |
children | af18922b416d |
rev | line source |
---|---|
396 | 1 # this should be used with nimscript |
2 | |
386
31f8ecc6644c
add: util to build projects more streamlined
Sam <sam@basx.dev>
parents:
diff
changeset
|
3 import std/strformat |
31f8ecc6644c
add: util to build projects more streamlined
Sam <sam@basx.dev>
parents:
diff
changeset
|
4 import std/os |
392
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
5 import std/strutils |
386
31f8ecc6644c
add: util to build projects more streamlined
Sam <sam@basx.dev>
parents:
diff
changeset
|
6 |
388 | 7 import ./core/audiotypes |
396 | 8 import ./core/constants |
388 | 9 |
1039 | 10 const BLENDER_CONVERT_SCRIPT = currentSourcePath().parentDir().parentDir().joinPath("tools/blender_gltf_converter.py") |
1075
e0635e0bfac3
fix: steamcmd not executable, implement steam-upload
sam <sam@basx.dev>
parents:
1073
diff
changeset
|
11 const STEAMCMD_ZIP = currentSourcePath().parentDir().parentDir().joinPath("tools/steamcmd.zip") |
1082 | 12 const STEAMBUILD_DIR_NAME = "steam" |
13 | |
14 var STEAMLIB: string | |
15 if defined(linux): | |
16 STEAMLIB = currentSourcePath().parentDir().parentDir().joinPath("libs/libsteam_api.so") | |
17 elif defined(windows): | |
18 STEAMLIB = currentSourcePath().parentDir().parentDir().joinPath("libs/steam_api.dll") | |
19 else: | |
20 raise newException(Exception, "Unsupported platform") | |
1120 | 21 let SQLITELIB_32 = currentSourcePath().parentDir().parentDir().joinPath("libs/sqlite3_32.dll") |
22 let SQLITELIB_64 = currentSourcePath().parentDir().parentDir().joinPath("libs/sqlite3_64.dll") | |
392
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
23 |
437 | 24 proc semicongine_builddir*(buildname: string, builddir = "./build"): string = |
1104
ed79529e70a3
fix: packaging fails if there are no resources, fix: zip-generation
sam <sam@basx.dev>
parents:
1102
diff
changeset
|
25 assert projectName() != "", "Please specify project file as a commandline argument" |
391
68198310770b
add: function to zip directories, usefull when creating dists
Sam <sam@basx.dev>
parents:
390
diff
changeset
|
26 var platformDir = "unkown" |
392
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
27 |
391
68198310770b
add: function to zip directories, usefull when creating dists
Sam <sam@basx.dev>
parents:
390
diff
changeset
|
28 if defined(linux): |
68198310770b
add: function to zip directories, usefull when creating dists
Sam <sam@basx.dev>
parents:
390
diff
changeset
|
29 platformDir = "linux" |
392
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
30 elif defined(windows): |
391
68198310770b
add: function to zip directories, usefull when creating dists
Sam <sam@basx.dev>
parents:
390
diff
changeset
|
31 platformDir = "windows" |
1084 | 32 else: |
33 raise newException(Exception, "Unsupported platform") | |
391
68198310770b
add: function to zip directories, usefull when creating dists
Sam <sam@basx.dev>
parents:
390
diff
changeset
|
34 |
68198310770b
add: function to zip directories, usefull when creating dists
Sam <sam@basx.dev>
parents:
390
diff
changeset
|
35 return builddir / buildname / platformDir / projectName() |
68198310770b
add: function to zip directories, usefull when creating dists
Sam <sam@basx.dev>
parents:
390
diff
changeset
|
36 |
437 | 37 proc semicongine_build_switches*(buildname: string, builddir = "./build") = |
386
31f8ecc6644c
add: util to build projects more streamlined
Sam <sam@basx.dev>
parents:
diff
changeset
|
38 switch("experimental", "strictEffects") |
31f8ecc6644c
add: util to build projects more streamlined
Sam <sam@basx.dev>
parents:
diff
changeset
|
39 switch("experimental", "strictFuncs") |
31f8ecc6644c
add: util to build projects more streamlined
Sam <sam@basx.dev>
parents:
diff
changeset
|
40 switch("define", "nimPreviewHashRef") |
1084 | 41 if defined(linux): |
42 switch("define", "VK_USE_PLATFORM_XLIB_KHR") | |
43 elif defined(windows): | |
400 | 44 switch("define", "VK_USE_PLATFORM_WIN32_KHR") |
45 switch("app", "gui") | |
1084 | 46 else: |
47 raise newException(Exception, "Unsupported platform") | |
48 | |
437 | 49 switch("outdir", semicongine_builddir(buildname, builddir = builddir)) |
1082 | 50 switch("passL", "-Wl,-rpath,'$ORIGIN'") # adds directory of executable to dynlib search path |
386
31f8ecc6644c
add: util to build projects more streamlined
Sam <sam@basx.dev>
parents:
diff
changeset
|
51 |
1082 | 52 proc semicongine_pack*(outdir: string, bundleType: string, resourceRoot: string, withSteam: bool) = |
438 | 53 switch("define", "PACKAGETYPE=" & bundleType) |
386
31f8ecc6644c
add: util to build projects more streamlined
Sam <sam@basx.dev>
parents:
diff
changeset
|
54 |
1088 | 55 assert resourceRoot.dirExists, &"Resource root '{resourceRoot}' does not exists" |
56 | |
1075
e0635e0bfac3
fix: steamcmd not executable, implement steam-upload
sam <sam@basx.dev>
parents:
1073
diff
changeset
|
57 outdir.rmDir() |
e0635e0bfac3
fix: steamcmd not executable, implement steam-upload
sam <sam@basx.dev>
parents:
1073
diff
changeset
|
58 outdir.mkDir() |
396 | 59 |
60 echo "BUILD: Packing assets from '" & resourceRoot & "' into directory '" & outdir & "'" | |
61 let outdir_resources = joinPath(outdir, RESOURCEROOT) | |
62 if bundleType == "dir": | |
63 cpDir(resourceRoot, outdir_resources) | |
64 elif bundleType == "zip": | |
1075
e0635e0bfac3
fix: steamcmd not executable, implement steam-upload
sam <sam@basx.dev>
parents:
1073
diff
changeset
|
65 outdir_resources.mkDir() |
1088 | 66 for resourceDir in resourceRoot.listDirs(): |
67 let outputfile = joinPath(outdir_resources, resourceDir.splitPath().tail & ".zip") | |
68 withdir resourceDir: | |
396 | 69 if defined(linux): |
1088 | 70 echo &"zip -r {relativePath(outputfile, resourceDir)} ." |
71 exec &"zip -r {relativePath(outputfile, resourceDir)} ." | |
396 | 72 elif defined(windows): |
1088 | 73 echo &"powershell Compress-Archive * {relativePath(outputfile, resourceDir)}" |
74 exec &"powershell Compress-Archive * {relativePath(outputfile, resourceDir)}" | |
1084 | 75 else: |
76 raise newException(Exception, "Unsupported platform") | |
437 | 77 elif bundleType == "exe": |
78 switch("define", "BUILD_RESOURCEROOT=" & joinPath(getCurrentDir(), resourceRoot)) # required for in-exe packing of resources, must be absolute | |
1102 | 79 if defined(windows): |
1120 | 80 SQLITELIB_32.cpFile(outdir.joinPath(SQLITELIB_32.extractFilename)) |
81 SQLITELIB_64.cpFile(outdir.joinPath(SQLITELIB_64.extractFilename)) | |
1082 | 82 if withSteam: |
83 STEAMLIB.cpFile(outdir.joinPath(STEAMLIB.extractFilename)) | |
391
68198310770b
add: function to zip directories, usefull when creating dists
Sam <sam@basx.dev>
parents:
390
diff
changeset
|
84 |
68198310770b
add: function to zip directories, usefull when creating dists
Sam <sam@basx.dev>
parents:
390
diff
changeset
|
85 proc semicongine_zip*(dir: string) = |
68198310770b
add: function to zip directories, usefull when creating dists
Sam <sam@basx.dev>
parents:
390
diff
changeset
|
86 withdir dir.parentDir: |
1125 | 87 let zipFile = dir.lastPathPart & ".zip" |
88 if zipFile.fileExists: | |
89 zipFile.rmFile() | |
391
68198310770b
add: function to zip directories, usefull when creating dists
Sam <sam@basx.dev>
parents:
390
diff
changeset
|
90 if defined(linux): |
1104
ed79529e70a3
fix: packaging fails if there are no resources, fix: zip-generation
sam <sam@basx.dev>
parents:
1102
diff
changeset
|
91 exec &"zip -r {dir.lastPathPart} {dir.lastPathPart}" |
391
68198310770b
add: function to zip directories, usefull when creating dists
Sam <sam@basx.dev>
parents:
390
diff
changeset
|
92 elif defined(windows): |
68198310770b
add: function to zip directories, usefull when creating dists
Sam <sam@basx.dev>
parents:
390
diff
changeset
|
93 exec &"powershell Compress-Archive * {dir.lastPathPart}" |
1084 | 94 else: |
95 raise newException(Exception, "Unsupported platform") | |
391
68198310770b
add: function to zip directories, usefull when creating dists
Sam <sam@basx.dev>
parents:
390
diff
changeset
|
96 |
392
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
97 |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
98 # need this because fileNewer from std/os does not work in Nim VM |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
99 proc fileNewerStatic(file1, file2: string): bool = |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
100 assert file1.fileExists |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
101 assert file2.fileExists |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
102 when defined(linux): |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
103 let command = "/usr/bin/test " & file1 & " -nt " & file2 |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
104 let ex = gorgeEx(command) |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
105 return ex.exitCode == 0 |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
106 elif defined(window): |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
107 {.error "Resource imports not supported on windows for now".} |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
108 |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
109 proc import_meshes*(files: seq[(string, string)]) = |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
110 if files.len == 0: |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
111 return |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
112 |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
113 var args = @["--background", "--python", BLENDER_CONVERT_SCRIPT, "--"] |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
114 for (input, output) in files: |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
115 args.add input |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
116 args.add output |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
117 |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
118 exec("blender " & args.join(" ")) |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
119 |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
120 proc import_audio*(files: seq[(string, string)]) = |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
121 for (input, output) in files: |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
122 let command = "ffmpeg " & ["-y", "-i", input, "-ar", $AUDIO_SAMPLE_RATE, output].join(" ") |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
123 exec(command) |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
124 |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
125 proc semicongine_import_resource_file*(resourceMap: openArray[(string, string)]) = |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
126 when not defined(linux): |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
127 {.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.".} |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
128 return |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
129 var meshfiles: seq[(string, string)] |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
130 var audiofiles: seq[(string, string)] |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
131 |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
132 for (target_rel, source_rel) in resourceMap: |
1076
bfcec39dfb0b
fix: steamcmd and build-script location
sam <sam@basx.dev>
parents:
1075
diff
changeset
|
133 let target = thisDir().joinPath(target_rel) |
bfcec39dfb0b
fix: steamcmd and build-script location
sam <sam@basx.dev>
parents:
1075
diff
changeset
|
134 let source = thisDir().joinPath(source_rel) |
392
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
135 if not source.fileExists: |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
136 raise newException(IOError, &"Not found: {source}") |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
137 if not target.fileExists or source.fileNewerStatic(target): |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
138 echo &"{target} is outdated" |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
139 if source.endsWith("blend"): |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
140 meshfiles.add (source, target) |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
141 elif source.endsWith("mp3") or source.endsWith("ogg") or source.endsWith("wav"): |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
142 audiofiles.add (source, target) |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
143 else: |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
144 raise newException(Exception, &"unkown file type: {source}") |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
145 target.parentDir().mkDir() |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
146 else: |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
147 echo &"{target} is up-to-date" |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
148 import_meshes meshfiles |
ff751cbe66e3
add: build-time code for resouce importing
Sam <sam@basx.dev>
parents:
391
diff
changeset
|
149 import_audio audiofiles |
1073
56e3fb525527
add: initial code for automated steam builds
sam <sam@basx.dev>
parents:
1039
diff
changeset
|
150 |
56e3fb525527
add: initial code for automated steam builds
sam <sam@basx.dev>
parents:
1039
diff
changeset
|
151 |
1075
e0635e0bfac3
fix: steamcmd not executable, implement steam-upload
sam <sam@basx.dev>
parents:
1073
diff
changeset
|
152 # for steam-buildscript docs see https://partner.steamgames.com/doc/sdk/uploading |
e0635e0bfac3
fix: steamcmd not executable, implement steam-upload
sam <sam@basx.dev>
parents:
1073
diff
changeset
|
153 proc semicongine_steam_upload*(steamaccount, password, buildscript: string) = |
1082 | 154 let steamdir = thisDir().joinPath(STEAMBUILD_DIR_NAME) |
1073
56e3fb525527
add: initial code for automated steam builds
sam <sam@basx.dev>
parents:
1039
diff
changeset
|
155 if not dirExists(steamdir): |
1075
e0635e0bfac3
fix: steamcmd not executable, implement steam-upload
sam <sam@basx.dev>
parents:
1073
diff
changeset
|
156 steamdir.mkDir |
e0635e0bfac3
fix: steamcmd not executable, implement steam-upload
sam <sam@basx.dev>
parents:
1073
diff
changeset
|
157 let zipFilename = STEAMCMD_ZIP.extractFilename |
e0635e0bfac3
fix: steamcmd not executable, implement steam-upload
sam <sam@basx.dev>
parents:
1073
diff
changeset
|
158 STEAMCMD_ZIP.cpFile(steamdir.joinPath(zipFilename)) |
e0635e0bfac3
fix: steamcmd not executable, implement steam-upload
sam <sam@basx.dev>
parents:
1073
diff
changeset
|
159 withDir(steamdir): |
1084 | 160 if defined(linux): |
161 exec &"unzip {zipFilename}" | |
162 rmFile zipFilename | |
163 exec "steamcmd/steamcmd.sh +quit" # self-update steamcmd | |
164 elif defined(windows): | |
165 exec &"powershell Expand-Archive -LiteralPath {zipFilename} ." | |
166 rmFile zipFilename | |
167 exec "steamcmd/steamcmd.exe +quit" # self-update steamcmd | |
168 else: | |
169 raise newException(Exception, "Unsupported platform") | |
1075
e0635e0bfac3
fix: steamcmd not executable, implement steam-upload
sam <sam@basx.dev>
parents:
1073
diff
changeset
|
170 |
1084 | 171 var steamcmd: string |
172 if defined(linux): | |
1082 | 173 steamcmd = STEAMBUILD_DIR_NAME.joinPath("steamcmd").joinPath("steamcmd.sh") |
1084 | 174 elif defined(windows): |
175 steamcmd = STEAMBUILD_DIR_NAME.joinPath("steamcmd").joinPath("steamcmd.exe") | |
176 else: | |
177 raise newException(Exception, "Unsupported platform") | |
178 let scriptPath = "..".joinPath("..").joinPath(buildscript) | |
1076
bfcec39dfb0b
fix: steamcmd and build-script location
sam <sam@basx.dev>
parents:
1075
diff
changeset
|
179 exec &"./{steamcmd} +login \"{steamaccount}\" \"{password}\" +run_app_build {scriptPath} +quit" |
1126
881be2633761
add: command to sign executables on windows
sam <sam@basx.dev>
parents:
1125
diff
changeset
|
180 |
881be2633761
add: command to sign executables on windows
sam <sam@basx.dev>
parents:
1125
diff
changeset
|
181 proc semicongine_sign_executable*(file: string) = |
881be2633761
add: command to sign executables on windows
sam <sam@basx.dev>
parents:
1125
diff
changeset
|
182 const SIGNTOOL_EXE = "C:/Program Files (x86)/Windows Kits/10/App Certification Kit/signtool.exe" |
881be2633761
add: command to sign executables on windows
sam <sam@basx.dev>
parents:
1125
diff
changeset
|
183 if not SIGNTOOL_EXE.fileExists: |
881be2633761
add: command to sign executables on windows
sam <sam@basx.dev>
parents:
1125
diff
changeset
|
184 raise newException(Exception, &"signtool.exe not found at ({SIGNTOOL_EXE}), please install the Windows SDK") |
1127 | 185 exec &"\"{SIGNTOOL_EXE}\" sign /a /tr http://timestamp.globalsign.com/tsa/r6advanced1 /fd SHA256 /td SHA256 {file}" |