Skip to content

Commit

Permalink
Add Emscripten system and emcc toolset support. (#2376)
Browse files Browse the repository at this point in the history
* Add Emscripten system and emcc toolset support.

* Add system docs.
  • Loading branch information
tritao authored Dec 16, 2024
1 parent 9b51da0 commit bdc4073
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 26 deletions.
15 changes: 13 additions & 2 deletions modules/gmake/_preload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
local p = premake
local project = p.project

local function defaultToolset()
local target = os.target()
if target == p.MACOSX then
return "clang"
elseif target == p.EMSCRIPTEN then
return "emmcc"
else
return "gcc"
end
end

---
-- The GNU make action, with support for the new platforms API
---
Expand All @@ -15,12 +26,12 @@
trigger = "gmake",
shortname = "GNU Make",
description = "Generate GNU makefiles for POSIX, MinGW, and Cygwin",
toolset = iif(os.target() == p.MACOSX, "clang", "gcc"),
toolset = defaultToolset(),

valid_kinds = { "ConsoleApp", "WindowedApp", "StaticLib", "SharedLib", "Utility", "Makefile", "None" },
valid_languages = { "C", "C++", "C#" },
valid_tools = {
cc = { "clang", "gcc", "cosmocc" },
cc = { "clang", "gcc", "cosmocc", "emcc" },
dotnet = { "mono", "msnet", "pnet" }
},

Expand Down
15 changes: 13 additions & 2 deletions modules/gmake2/_preload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,29 @@
local p = premake
local project = p.project

local function defaultToolset()
local target = os.target()
if target == p.MACOSX then
return "clang"
elseif target == p.EMSCRIPTEN then
return "emmcc"
else
return "gcc"
end
end

newaction {
trigger = "gmake2",
shortname = "Alternative GNU Make",
description = "Generate GNU makefiles for POSIX, MinGW, and Cygwin",
toolset = iif(os.target() == p.MACOSX, "clang", "gcc"),
toolset = defaultToolset(),

valid_kinds = { "ConsoleApp", "WindowedApp", "StaticLib", "SharedLib", "Utility", "Makefile", "None" },

valid_languages = { "C", "C++", "C#" },

valid_tools = {
cc = { "clang", "gcc", "cosmocc" },
cc = { "clang", "gcc", "cosmocc", "emcc" },
dotnet = { "mono", "msnet", "pnet" }
},

Expand Down
1 change: 1 addition & 0 deletions src/_manifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"tools/clang.lua",
"tools/mingw.lua",
"tools/cosmocc.lua",
"tools/emcc.lua",

-- Clean action
"actions/clean/_clean.lua",
Expand Down
33 changes: 22 additions & 11 deletions src/_premake_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
p.ARM,
p.ARM64,
p.RISCV64,
p.LOONGARCH64
p.LOONGARCH64,
p.WASM32,
p.WASM64
},
aliases = {
i386 = p.X86,
Expand Down Expand Up @@ -840,6 +842,7 @@
allowed = {
"aix",
"bsd",
"emscripten",
"haiku",
"ios",
"linux",
Expand Down Expand Up @@ -1284,16 +1287,17 @@
value = "VALUE",
description = "Generate files for a different operating system",
allowed = {
{ "aix", "IBM AIX" },
{ "bsd", "OpenBSD, NetBSD, or FreeBSD" },
{ "haiku", "Haiku" },
{ "hurd", "GNU/Hurd" },
{ "ios", "iOS" },
{ "linux", "Linux" },
{ "macosx", "Apple Mac OS X" },
{ "solaris", "Solaris" },
{ "uwp", "Microsoft Universal Windows Platform"},
{ "windows", "Microsoft Windows" },
{ "aix", "IBM AIX" },
{ "bsd", "OpenBSD, NetBSD, or FreeBSD" },
{ "emscripten", "Emscripten" },
{ "haiku", "Haiku" },
{ "hurd", "GNU/Hurd" },
{ "ios", "iOS" },
{ "linux", "Linux" },
{ "macosx", "Apple Mac OS X" },
{ "solaris", "Solaris" },
{ "uwp", "Microsoft Universal Windows Platform"},
{ "windows", "Microsoft Windows" },
}
}

Expand Down Expand Up @@ -1428,6 +1432,13 @@
filter { "system:darwin" }
toolset "clang"

filter { "system:emscripten" }
toolset "emcc"
architecture "wasm32"

filter { "system:emscripten", "kind:ConsoleApp or WindowedApp" }
targetextension ".wasm"

filter { "platforms:Win32" }
architecture "x86"

Expand Down
4 changes: 3 additions & 1 deletion src/base/_foundation.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
premake.GCC = "gcc"
premake.HAIKU = "haiku"
premake.ANDROID = "android"
premake.EMSCRIPTEN = "emscripten"
premake.IOS = "ios"
premake.LINUX = "linux"
premake.MACOSX = "macosx"
Expand Down Expand Up @@ -63,7 +64,8 @@
premake.ARM64 = "ARM64"
premake.RISCV64 = "RISCV64"
premake.LOONGARCH64 = "loongarch64"

premake.WASM32 = "wasm32"
premake.WASM64 = "wasm64"


---
Expand Down
21 changes: 11 additions & 10 deletions src/base/os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -827,16 +827,17 @@

os.systemTags =
{
["aix"] = { "aix", "posix", "desktop" },
["android"] = { "android", "mobile" },
["bsd"] = { "bsd", "posix", "desktop" },
["haiku"] = { "haiku", "posix", "desktop" },
["ios"] = { "ios", "darwin", "posix", "mobile" },
["linux"] = { "linux", "posix", "desktop" },
["macosx"] = { "macosx", "darwin", "posix", "desktop" },
["solaris"] = { "solaris", "posix", "desktop" },
["uwp"] = { "uwp", "windows", "desktop" },
["windows"] = { "windows", "win32", "desktop" },
["aix"] = { "aix", "posix", "desktop" },
["android"] = { "android", "mobile" },
["bsd"] = { "bsd", "posix", "desktop" },
["emscripten"] = { "emscripten", "web" },
["haiku"] = { "haiku", "posix", "desktop" },
["ios"] = { "ios", "darwin", "posix", "mobile" },
["linux"] = { "linux", "posix", "desktop" },
["macosx"] = { "macosx", "darwin", "posix", "desktop" },
["solaris"] = { "solaris", "posix", "desktop" },
["uwp"] = { "uwp", "windows", "desktop" },
["windows"] = { "windows", "win32", "desktop" },
}

function os.getSystemTags(name)
Expand Down
2 changes: 2 additions & 0 deletions src/tools/clang.lua
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@
architecture = {
x86 = "-m32",
x86_64 = "-m64",
WASM32 = "-m32",
WASM64 = "-m64",
},
fatalwarnings = {
Link = "-Wl,--fatal-warnings",
Expand Down
21 changes: 21 additions & 0 deletions src/tools/emcc.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--
-- emcc.lua
-- Emscripten emcc toolset.
-- Copyright (c) 2024 Premake project
--

local p = premake
local clang = p.tools.clang

p.tools.emcc = table.deepcopy(clang, {})
local emcc = p.tools.emcc

emcc.tools = {
cc = "emcc",
cxx = "em++",
ar = "emar"
}

function emcc.gettoolname(cfg, tool)
return emcc.tools[tool]
end
1 change: 1 addition & 0 deletions tests/_tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ return {

-- -- Toolset tests
"tools/test_dotnet.lua",
"tools/test_emcc.lua",
"tools/test_gcc.lua",
"tools/test_clang.lua",
"tools/test_msc.lua",
Expand Down
48 changes: 48 additions & 0 deletions tests/tools/test_emcc.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
--
-- tests/test_emcc.lua
-- Automated test suite for the emcc toolset interface.
-- Copyright (c) 2024 Premake project
--

local p = premake
local suite = test.declare("tools_emcc")

local emcc = p.tools.emcc


--
-- Setup/teardown
--

local wks, prj, cfg

function suite.setup()
wks, prj = test.createWorkspace()
system "emscripten"
end

local function prepare()
cfg = test.getconfig(prj, "Debug")
end


--
-- Check the selection of tools based on the target system.
--

function suite.tools_onDefault()
system "emscripten"
prepare()
test.isequal("wasm32", cfg.architecture)
test.isequal("emcc", emcc.gettoolname(cfg, "cc"))
test.isequal("em++", emcc.gettoolname(cfg, "cxx"))
test.isequal("emar", emcc.gettoolname(cfg, "ar"))
end

function suite.tools_onWASM64()
system "emscripten"
architecture "WASM64"
prepare()
test.isequal("wasm64", cfg.architecture)
end

2 changes: 2 additions & 0 deletions website/docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ architecture ("value")
* `ARM64`
* `RISCV64`
* `loongarch64`
* `wasm32`,
* `wasm64`,
* `armv5`: Only supported in VSAndroid projects
* `armv7`: Only supported in VSAndroid projects
* `aarch64`: Only supported in VSAndroid projects
Expand Down
3 changes: 3 additions & 0 deletions website/docs/system.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ If no system is specified, Premake will identify and target the current operatin
* aix
* android
* bsd
* [emscripten](https://emscripten.org/)
* [haiku](http://www.haiku-os.org)
* ios
* linux
Expand All @@ -23,6 +24,8 @@ If no system is specified, Premake will identify and target the current operatin
* windows
* xbox360

To note: `emscripten` at the moment is only supported for the `gmake` and `gmake2` actions.

### Applies To ###

Project configurations.
Expand Down

0 comments on commit bdc4073

Please sign in to comment.