Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port Premake to Cosmopolitan Libc #2274

Merged
merged 3 commits into from
Oct 3, 2024
Merged
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
23 changes: 23 additions & 0 deletions .github/workflows/ci-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,29 @@ jobs:
with:
name: premake-${{ matrix.msystem }}-${{ matrix.platform }}
path: bin\${{ matrix.config }}\
cosmopolitan:
runs-on: ubuntu-latest
strategy:
matrix:
config: [debug, release]
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: tritao/setup-cosmopolitan@v1.3
with:
version: '3.9.2'
- name: Build
run: make -f Bootstrap.mak cosmo CONFIG=${{ matrix.config }}
- name: Test
run: bin/${{ matrix.config }}/premake5 test --test-all
- name: Docs check
run: bin/${{ matrix.config }}/premake5 docs-check
- name: Upload Artifacts
if: matrix.config == 'release'
uses: actions/upload-artifact@v4
with:
name: premake-cosmopolitan-universal
path: bin/${{ matrix.config }}/premake5

# This job will be required for PRs to be merged.
# This should depend on (via needs) all jobs that need to be successful for the PR to be merged.
Expand Down
9 changes: 9 additions & 0 deletions Bootstrap.mak
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,12 @@ windows: windows-base

windows-msbuild: windows-base
msbuild /p:Configuration=$(CONFIG) /p:Platform=$(PLATFORM:x86=win32) .\build\bootstrap\Premake5.sln

cosmo-clean: nix-clean

cosmo: cosmo-clean
mkdir -p build/bootstrap
cosmocc -o build/bootstrap/premake_bootstrap -DPREMAKE_NO_BUILTIN_SCRIPTS -DLUA_USE_POSIX -DLUA_USE_DLOPEN -I"$(LUA_DIR)" -I"$(LUASHIM_DIR)" $(SRC) -lm -ldl -lrt
./build/bootstrap/premake_bootstrap embed
./build/bootstrap/premake_bootstrap --to=build/bootstrap --cc=cosmocc gmake2
$(MAKE) -C build/bootstrap -j`getconf _NPROCESSORS_ONLN` config=$(CONFIG)
4 changes: 4 additions & 0 deletions contrib/curl/lib/curl_setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@
# include "config-linux.h"
#endif

#ifdef __COSMOPOLITAN__
# include "config-linux.h"
#endif

#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
# include "config-linux.h"
#endif
Expand Down
2 changes: 1 addition & 1 deletion contrib/libzip/premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ project "zip-lib"
"**.c"
}

filter "system:linux or bsd or solaris or haiku"
filter "toolset:gcc or clang or cosmocc"
defines { "HAVE_SSIZE_T_LIBZIP", "HAVE_CONFIG_H" }
forceincludes { "unistd.h" }

Expand Down
2 changes: 1 addition & 1 deletion modules/gmake/_preload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
valid_kinds = { "ConsoleApp", "WindowedApp", "StaticLib", "SharedLib", "Utility", "Makefile", "None" },
valid_languages = { "C", "C++", "C#" },
valid_tools = {
cc = { "clang", "gcc" },
cc = { "clang", "gcc", "cosmocc" },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gmake is the "deprecated" generator, in favor to gmake2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know what's missing to unify both generators?

dotnet = { "mono", "msnet", "pnet" }
},

Expand Down
2 changes: 1 addition & 1 deletion modules/gmake2/_preload.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
valid_languages = { "C", "C++", "C#" },

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

Expand Down
15 changes: 8 additions & 7 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
defines { "LUA_USE_MACOSX" }
links { "CoreServices.framework", "Foundation.framework", "Security.framework", "readline" }

filter "system:linux"
filter { "system:linux", "toolset:not cosmocc" }
links { "uuid" }

filter { "system:macosx", "action:gmake" }
Expand Down Expand Up @@ -289,13 +289,14 @@
include "contrib/curl"
end

group "Binary Modules"
include "binmodules/example"

if not _OPTIONS["no-luasocket"] then
include "binmodules/luasocket"
end
if _OPTIONS["cc"] ~= "cosmocc" then
group "Binary Modules"
include "binmodules/example"

if not _OPTIONS["no-luasocket"] then
include "binmodules/luasocket"
end
end
--
-- A more thorough cleanup.
--
Expand Down
1 change: 1 addition & 0 deletions src/_manifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"tools/snc.lua",
"tools/clang.lua",
"tools/mingw.lua",
"tools/cosmocc.lua",

-- Clean action
"actions/clean/_clean.lua",
Expand Down
4 changes: 2 additions & 2 deletions src/host/os_getnumcpus.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include "premake.h"

#if PLATFORM_LINUX
#if PLATFORM_LINUX | PLATFORM_COSMO
#include <sched.h>
#elif PLATFORM_SOLARIS | PLATFORM_AIX | PLATFORM_MACOSX | PLATFORM_BSD
#include <sys/sysctl.h>
Expand All @@ -25,7 +25,7 @@ int do_getnumcpus()
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
#elif PLATFORM_LINUX
#elif PLATFORM_LINUX | PLATFORM_COSMO
cpu_set_t set;
int count, i;

Expand Down
7 changes: 6 additions & 1 deletion src/host/os_getpass.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ int os_getpass(lua_State* L)

lua_pushstring(L, buffer);
return 1;
#else
#elif PLATFORM_COSMO
luaL_error(L, "Not supported by this platform");
return 0;
#elif PLATFORM_POSIX
lua_pushstring(L, getpass(prompt));
return 1;
#else
#error Not implemented yet for this platform
#endif
}
2 changes: 1 addition & 1 deletion src/host/os_getversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ int getversion(struct OsVersionInfo* info)

/*************************************************************/

#elif defined(PLATFORM_BSD) || defined(PLATFORM_LINUX) || defined(PLATFORM_SOLARIS) || defined(PLATFORM_HURD) || defined(PLATFORM_HAIKU)
#elif defined(PLATFORM_BSD) || defined(PLATFORM_LINUX) || defined(PLATFORM_SOLARIS) || defined(PLATFORM_HURD) || defined(PLATFORM_HAIKU) || defined(PLATFORM_COSMO)

#include <string.h>
#include <sys/utsname.h>
Expand Down
31 changes: 30 additions & 1 deletion src/host/premake.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* \author Copyright (c) 2002-2017 Jason Perkins and the Premake project
*/

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
Expand All @@ -19,6 +20,10 @@
#include <sys/sysctl.h>
#endif

#if PLATFORM_COSMO
#include <cosmo.h>
#endif

#define ERROR_MESSAGE "Error: %s\n"


Expand Down Expand Up @@ -172,6 +177,24 @@ void luaL_register(lua_State *L, const char *libname, const luaL_Reg *l)
}


static const char* premake_host_os()
{
#if PLATFORM_COSMO
if (IsLinux()) { return "linux"; }
else if (IsWindows()) { return "windows"; }
else if (IsXnu()) { return "macosx"; }
else if (IsBsd()) { return "bsd"; }
else
{
assert(0 && "Platform is unknown to Cosmopolitan Libc");
return 0;
}
#else
return PLATFORM_OS;
#endif
}


/**
* Initialize the Premake Lua environment.
*/
Expand Down Expand Up @@ -213,13 +236,19 @@ int premake_init(lua_State* L)
lua_setglobal(L, "_PREMAKE_URL");

/* set the target OS platform variable */
lua_pushstring(L, PLATFORM_OS);
lua_pushstring(L, premake_host_os());
lua_setglobal(L, "_TARGET_OS");

/* set the target arch platform variable */
lua_pushnil(L);
lua_setglobal(L, "_TARGET_ARCH");

#if PLATFORM_COSMO
/* set _COSMOPOLITAN if its a Cosmopolitan build */
lua_pushboolean(L, TRUE);
lua_setglobal(L, "_COSMOPOLITAN");
#endif

/* find the user's home directory */
value = getenv("HOME");
if (!value) value = getenv("USERPROFILE");
Expand Down
5 changes: 4 additions & 1 deletion src/host/premake.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@
#elif defined (__GNU__)
#define PLATFORM_HURD (1)
#define PLATFORM_OS "hurd"
#elif defined (__COSMOPOLITAN__)
#define PLATFORM_COSMO (1)
#define PLATFORM_OS "cosmopolitan"
samsinsane marked this conversation as resolved.
Show resolved Hide resolved
#else
#define PLATFORM_WINDOWS (1)
#define PLATFORM_OS "windows"
#endif

#define PLATFORM_POSIX (PLATFORM_LINUX || PLATFORM_BSD || PLATFORM_MACOSX || PLATFORM_SOLARIS || PLATFORM_HAIKU)
#define PLATFORM_POSIX (PLATFORM_LINUX || PLATFORM_BSD || PLATFORM_MACOSX || PLATFORM_SOLARIS || PLATFORM_HAIKU || PLATFORM_COSMO)

#if defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) || \
defined(_M_X64) || defined(_M_AMD64)
Expand Down
27 changes: 27 additions & 0 deletions src/tools/cosmocc.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
--
-- cosmocc.lua
-- Cosmopolitan Libc toolset adapter for Premake
-- Copyright (c) 2024 Premake project
--

local p = premake
local gcc = p.tools.gcc

p.tools.cosmocc = table.deepcopy(gcc, {})
local cosmocc = p.tools.cosmocc

function cosmocc.getsharedlibarg(cfg)
return ""
end

cosmocc.ldflags.kind.SharedLib = cosmocc.getsharedlibarg

cosmocc.tools = {
cc = "cosmocc",
cxx = "cosmoc++",
ar = "cosmoar",
}

function cosmocc.gettoolname(cfg, tool)
return cosmocc.tools[tool]
tritao marked this conversation as resolved.
Show resolved Hide resolved
end
3 changes: 3 additions & 0 deletions tests/base/test_binmodules.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
local suite = test.declare("premake_binmodules")
local p = premake

if not _COSMOPOLITAN then

function suite.setup()
require("example")
Expand All @@ -17,3 +18,5 @@
local result = example.test("world")
test.isequal("hello world", result)
end

end