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