Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,4 @@ tools/WindowsUWP/x86/libCompilerRt.lib
*.a
*.bc
*.lib
*.o
57 changes: 33 additions & 24 deletions tools/np-build-driver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ local exclude_files = {}
local directory = ""

common_flags = "-ffast-math -fno-threadsafe-statics -nostdlibinc -nobuiltininc -nostdinc++ -Wno-macro-redefined -I.."..SLASH.."NativePath -I.."..SLASH.."NativePath"..SLASH.."standard"
zig_common_flags = "-Wno-ignored-attributes -Wno-delete-non-virtual-dtor -Wno-macro-redefined -fno-rtti -fno-exceptions"

objs = {}
cfiles = {}
Expand All @@ -60,33 +61,41 @@ function table.contains(table, element)
return false
end

-- Process the zig command line options
for i,v in ipairs(arg) do
if v == "debug" then
debug = true
elseif string.starts(v, "-I") then
local includeDir = string.sub(v, 3)
common_flags = common_flags.." -I"..includeDir
elseif string.starts(v, "-D") then
local includeDir = string.sub(v, 3)
common_flags = common_flags.." -D"..includeDir
elseif string.starts(v, "-n") then
local n = string.sub(v, 3)
outputName = n
elseif string.starts(v, "-E") then
local n = string.sub(v, 3)
table.insert(exclude_dirs, n)
elseif string.starts(v, "-e") then
local n = string.sub(v, 3)
table.insert(exclude_files, n)
elseif v == "--verbose" or v == "-v" then
is_verbose = true
elseif string.starts(v, "-p") then
platform = string.sub(v, 3)
else
directory = v
end
if v == "debug" then
debug = true
elseif string.starts(v, "-I") then
-- add an include directory to the compiler flags
local includeDir = string.sub(v, 3)
common_flags = common_flags.." -I"..includeDir
zig_common_flags = zig_common_flags.." -I"..includeDir
elseif string.starts(v, "-D") then
-- add a define to the compiler flags
local includeDir = string.sub(v, 3)
common_flags = common_flags.." -D"..includeDir
zig_common_flags = zig_common_flags.." -D"..includeDir
elseif string.starts(v, "-n") then
local n = string.sub(v, 3)
outputName = n
elseif string.starts(v, "-E") then
local n = string.sub(v, 3)
table.insert(exclude_dirs, n)
elseif string.starts(v, "-e") then
local n = string.sub(v, 3)
table.insert(exclude_files, n)
elseif v == "--verbose" or v == "-v" then
is_verbose = true
elseif v == "--shared" then
is_shared = true
elseif string.starts(v, "-p") then
platform = string.sub(v, 3)
else
directory = v
end
end

-- Build a list of all source files to compile excluding those that we do not want via -E or -e see above.
for filename, attr in dirtree(directory) do
if table.contains(exclude_dirs, filename) ~= true and table.contains(exclude_files, filename) ~= true then
if string.ends(filename, ".c") and attr.mode == "file" and table.contains(exclude_files, filename) ~= true then
Expand Down
145 changes: 145 additions & 0 deletions tools/np-build-stride.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
-- Special trick to load the parent path of the current script.
local parent_path = string.match(arg[0], "(.-)([^\\/]-%.?([^%.\\/]*))$")
-- Add that path to the list of places where lua will load packages
package.path = package.path .. ";" .. parent_path .. "?.lua"
-- Load the driver package which will take care of the command line arguments and other settings
-- as here we just focus on the code necessary to build our supported platforms
require "np-build-driver"

local debug_flags = "-O0 -g"
local debug_ms_flags = "-Od"
local release_flags = "-O3"
local release_ms_flags = "-O2"

-- NOTE: This is a cleaned up version of np-build.lua specific to the needs of Stride, and uses Zig CC cross compiler instead of clang, etc.
-- We can port over other commands from np-build.lua as needed.

-- we require Zig v0.12.0 reachable from your path. this only works on Windows for now
local zig = "zig.exe"

-- For testing alternative versions of Zig, etc
-- local zig = "c:\\devtools\\zig-0.11.0\\zig.exe"

function BuildZig(cfile, target, platform_args)
local flags = ""
if debug then flags = debug_flags else flags = release_flags end

local cmd = zig.." cc "..platform_args.." -target "..target.." "..zig_common_flags.." "..flags.." -o "..cfile..".o ".." -c "..cfile
if is_verbose == true then
print(cmd)
end

os.execute(cmd)
table.insert(objs, cfile..".o")
end

function LinkZigStatic(target, folder, ext)
local objs_str = ""
for i, o in ipairs(objs) do
objs_str = objs_str..o.." "
end
local cmd = zig.." build-lib -static -target "..target.." -femit-bin="..folder.."\\"..outputName.."."..ext.." "..objs_str
if is_verbose == true then
print(cmd)
end
os.execute(cmd)
end

-- TODO: Unused for now
-- function LinkZigShared(target, folder, ext)
-- local objs_str = ""
-- for i, o in ipairs(objs) do
-- objs_str = objs_str..o.." "
-- end

-- local cmd = zig.." build-lib -shared -target "..target.." -femit-bin="..folder.."\\"..outputName.."."..ext.." "..objs_str
-- if is_verbose == true then
-- print(cmd)
-- end
-- os.execute(cmd)
-- end

if platform == "windows" then
lfs.mkdir("libs")
lfs.chdir("libs")
lfs.mkdir("win-x86")
lfs.mkdir("win-x64")
lfs.mkdir("win-arm64")
lfs.chdir("..")

objs = {}
print ("Building Windows x86...")
for i,f in ipairs(cfiles) do
BuildZig(f, "x86-windows-gnu", "-DNP_WIN32 -m32 -gcodeview -fno-ms-extensions")
end
LinkZigStatic("x86-windows-gnu", "libs\\win-x86", "lib")

objs = {}
print ("Building Windows x64...")
for i,f in ipairs(cfiles) do
BuildZig(f, "x86_64-windows-gnu", "-DNP_WIN32 -m64 -gcodeview -fno-ms-extensions")
end
LinkZigStatic("x86_64-windows-gnu", "libs\\win-x64", "lib")

objs = {}
print ("Building Windows arm64...")
for i,f in ipairs(cfiles) do
BuildZig(f, "aarch64-windows-gnu", "-DNP_WIN32 -m64 -gcodeview -fno-ms-extensions")
end
LinkZigStatic("aarch64-windows-gnu", "libs\\win-arm64", "lib")

elseif platform == "ios" then
-- TODO: port over to Zig

elseif platform == "macos" then
lfs.mkdir("libs")
lfs.chdir("libs")
lfs.mkdir("osx-x64")
lfs.mkdir("osx-arm64")
lfs.chdir("..")

objs = {}
print ("Building macOS x64...")
for i,f in ipairs(cfiles) do
BuildZig(f, "x86_64-macos", "-DNP_MACOS -mmacosx-version-min=10.5")
end
LinkZigStatic("x86_64-macos", "libs\\osx-x64", "a")

objs = {}
print ("Building macOS arm64...")
for i,f in ipairs(cfiles) do
BuildZig(f, "aarch64-macos", "-DNP_MACOS -mmacosx-version-min=10.5")
end
LinkZigStatic("aarch64-macos", "libs\\osx-arm64", "a")

-- TODO: Not sure we need lipo anymore? since we target each runtime separately, and only x64/aarch64
-- lfs.mkdir("macOS")
-- os.execute("lipo macOS\\"..outputName.."_i386.a macOS\\"..outputName.."_x86_64.a -create -output macOS\\"..outputName..".a")
-- os.remove("macOS\\"..outputName.."_i386.a")
-- os.remove("macOS\\"..outputName.."_x86_64.a")

elseif platform == "linux" then
lfs.mkdir("libs")
lfs.chdir("libs")
lfs.mkdir("linux-x64")
lfs.mkdir("linux-arm64")
lfs.chdir("..")

objs = {}
print ("Building Linux x64...")
for i,f in ipairs(cfiles) do
BuildZig(f, "x86_64-linux-gnu", "-DNP_LINUX -fPIC")
end
LinkZigStatic("x86_64-linux-gnu", "libs\\linux-x64", "a")

objs = {}
print ("Building Linux arm64...")
for i,f in ipairs(cfiles) do
BuildZig(f, "aarch64-linux-gnu", "-DNP_LINUX -fPIC")
end
LinkZigStatic("aarch64-linux-gnu", "libs\\linux-arm64", "a")

elseif platform == "android" then
-- TODO: port over to Zig

end