Skip to content

Conversation

@illwieckz
Copy link
Member

@illwieckz illwieckz commented Apr 7, 2025

I added some CMakeLists.tx code to rebuild sel_ldr and nacl_helper_bootstrap.

This relies on some unified DaemonPlatform framework copied from DaemonEngine/Daemon#1641 on purpose to give this CMake code the same easiness at doing cross-compiled builds.

I need help to complete the src/trusted/service_runtime/CMakeLists.txt file.

The CMakeLists.txt files are a rewrite of the file SConstruct and *.scons files with all unit tests deleted. Remaining unported code is commented out with lines starting with #TODO:.

Build status:

system arch build sel_ldr build nacl_helper_bootstrap run helloworld nexe
windows amd64 not tested N/A not tested
windows i686 not tested N/A not tested
mingw amd64 ✅️ N/A not tested
mingw i686 ✅️ N/A not tested
linux amd64 ✅️ ✅️ ✅️
linux i686 ✅️ ✅️ not tested
linux armhf ✅️ ✅️ not tested
linux armhf 16k ✅️ ✅️ not tested
linux armel ✅️ ✅️ not tested
linux mips ✅️ ✅️ not tested
android armel ✅️ ✅️ ❌️
macos amd64 ✅️ N/A not tested

Dynamically linked loader:

system arch shared link sel_ldr run helloworld nexe
windows amd64 not tested not tested
windows i686 not tested not tested
mingw amd64 ✅️ not tested
mingw i686 ✅️ not tested
linux amd64 ✅️ ✅️
linux i686 ✅️ not tested
linux armhf ✅️ ✅️
linux armhf 16k ✅️ not tested
linux armel ✅️ not tested
linux mips ✅️ not tested
android armel ✅️ not tested
macos amd64 ✅️ not tested

Statically linked loader: NOW REMOVED.

system arch static link sel_ldr run helloworld nexe
windows amd64 not implemented not implemented
windows i686 not implemented not implemented
mingw amd64 ✅️ not tested
mingw i686 ✅️ not tested
linux amd64 ✅️ ✅️
linux i686 ✅️ not tested
linux armhf ✅️ ❌️ segfault
linux armhf 16k ✅️ not tested
linux armel ✅️ not tested
linux mips ✅️ not tested
android armel ✅️ not tested
macos amd64 ✅️ not tested

Things like linux-mips were tested only because I ported the SCons code to CMake for completeness and make sure I forgot nothing.

I tested android-armel because I remember that in the past @cu-kai tried to get daemon-tty running on Android to get a console for his server.

@illwieckz illwieckz marked this pull request as draft April 7, 2025 18:42
@illwieckz illwieckz force-pushed the illwieckz/cmake branch 2 times, most recently from 2775579 to 993144e Compare April 7, 2025 20:34
@slipher
Copy link
Member

slipher commented Apr 7, 2025

Is it really so bad to use Scons? Unlike CMake, it can handle multiple toolchains, which makes it well-suited for this repo. Also if we keep the same build system, we can easily compare things between our version and upstream.

@illwieckz
Copy link
Member Author

illwieckz commented Apr 7, 2025

Is it really so bad to use Scons?

Yes. 😅️

Unlike CMake, it can handle multiple toolchains, which makes it well-suited for this repo.

Uh, totally not. The Scons scripts are not compatible with cross-compiling to begin with.

First purpose of this effort is to make possible to use multiple toolchains.

@slipher
Copy link
Member

slipher commented Apr 7, 2025

The Scons scripts are not compatible with cross-compiling to begin with.

Wrong. With Chromium native_client I can do ./scons --mode=nacl,opt-linux platform=x86-32 sel_ldr irt_core_raw or ./scons --mode=nacl,opt-linux platform=arm sel_ldr irt_core_raw and everything completes successfully and produces executables of the expected architectures.

@illwieckz
Copy link
Member Author

How do I build with MinGW for Windows on Linux? For Linux Arm?

How do I make a static nacl_loader? How do I rebuild with a 16k PageSize for Arm?

This scons stuff is over-convoluted…

@illwieckz
Copy link
Member Author

illwieckz commented Apr 7, 2025

Wrong. With Chromium native_client I can do ./scons --mode=nacl,opt-linux platform=arm sel_ldr

With this exact command I get that:

Exception: Cannot find a toolchain for arm in toolchain/linux_x86/pnacl_newlib_raw:
  File "SConstruct", line 2799:
    if UsingNaclMode(): nacl_env = nacl_env.Clone(
  File "/usr/lib/python3/dist-packages/SCons/Environment.py", line 1610:
    apply_tools(clone, tools, toolpath)
  File "/usr/lib/python3/dist-packages/SCons/Environment.py", line 117:
    _ = env.Tool(tool)
  File "/usr/lib/python3/dist-packages/SCons/Environment.py", line 2033:
    tool(self)
  File "/usr/lib/python3/dist-packages/SCons/Tool/__init__.py", line 265:
    self.generate(env, *args, **kw)
  File "site_scons/site_tools/naclsdk.py", line 756:
    _SetEnvForNativeSdk(env, root)
  File "site_scons/site_tools/naclsdk.py", line 109:
    raise Exception("Cannot find a toolchain for %s in %s" %

I have arm-linux-gnueabihf-gcc (from the gcc-arm-linux-gnueabihf package), and also clang.

@slipher
Copy link
Member

slipher commented Apr 7, 2025

How do I build with MinGW for Windows on Linux?

Found some documentation: https://github.com/DaemonEngine/native_client/blob/master/docs/build_systems.md. Although cross-architecture builds are supported, cross-OS builds are not. And you can't build with MinGW at all as it is designed for the MSVC toolchain. It seems building with MinGW would imply a porting effort beyond just the build system, as there is, e.g., a Microsoft assembler file.

The lack of cross-OS support would seem to be a limitation of Native Client though, not a limitation of Scons.

For Linux Arm?

I posted that in the previous message.

Wrong. With Chromium native_client I can do ./scons --mode=nacl,opt-linux platform=arm sel_ldr

With this exact command I get that:

You're getting an error finding a PNaCl toolchain, which should have been downloaded by gclient or whatever. Make sure you are in a fully equipped Chromium environment.

@illwieckz
Copy link
Member Author

Wrong. With Chromium native_client I can do ./scons --mode=nacl,opt-linux platform=arm sel_ldr

With this exact command I get that:

You're getting an error finding a PNaCl toolchain, which should have been downloaded by gclient or whatever. Make sure you are in a fully equipped Chromium environment.

Why do I need a PNaCl toolchain to build an Arm sel_ldr?

The lack of cross-OS support would seem to be a limitation of Native Client though, not a limitation of Scons.

Yes, all that scons code in the repository is not meant for cross-compilation, that's what I meant.

And you can't build with MinGW at all as it is designed for the MSVC toolchain. It seems building with MinGW would imply a porting effort beyond just the build system, as there is, e.g., a Microsoft assembler file.

Very annoying… I have seen they also have some Cygwin code, so I wonder if that can be used on MinGW, I haven't looked at this deeply though.

@illwieckz
Copy link
Member Author

Sorry, the output of ./scons --mode=opt-linux platform=arm sel_ldr is (I forgot to remove the nacl mode):

AttributeError: 'SConsEnvironment' object has no attribute 'Program':
  File "SConstruct", line 3889:
    BuildEnvironments(selected_envs)
  File "site_init", line 198:
    
  File "/usr/lib/python3/dist-packages/SCons/Util/envs.py", line 242:
    return self.method(*nargs, **kwargs)
  File "site_scons/site_tools/defer.py", line 148:
    func(env)
  File "site_init", line 125:
    
  File "/usr/lib/python3/dist-packages/SCons/Script/SConscript.py", line 598:
    return _SConscript(self.fs, *files, **subst_kw)
  File "/usr/lib/python3/dist-packages/SCons/Script/SConscript.py", line 285:
    exec(compile(scriptdata, scriptname, 'exec'), call_stack[-1].globals)
  File "src/trusted/validator_arm/build.scons", line 271:
    nexe = untrusted_env.ComponentProgram(test, 'testdata/' + test + '.S',
  File "/usr/lib/python3/dist-packages/SCons/Util/envs.py", line 242:
    return self.method(*nargs, **kwargs)
  File "site_scons/site_tools/component_builders.py", line 485:
    out_nodes = env.Program(prog_name, *args, **kwargs)

@illwieckz
Copy link
Member Author

This can already build sel_ldr on linux-amd64.

@illwieckz
Copy link
Member Author

This can now rebuild sel_ldr on linux-armhf.

@illwieckz
Copy link
Member Author

This can now rebuild sel_ldr on linux-i686.

@illwieckz
Copy link
Member Author

This can now rebuild nacl_helper_bootstrap on both linux-amd64, linux-i686 and linux-armhf.

@illwieckz
Copy link
Member Author

This can now rebuild sel_ldr on macos-amd64.

@illwieckz
Copy link
Member Author

illwieckz commented Apr 8, 2025

By using JWasm as a MASM-compatible assembler I can cross-build (from Linux to Windows with MinGW) the sel_ldr binary for windows-amd64 as long as I put ksamd64.inc, kxamd64.inc and macamd64.inc in ../../src/trusted/service_runtime/arch/x86_64.

I got them from:

@illwieckz illwieckz force-pushed the illwieckz/cmake branch 2 times, most recently from 37f0e62 to 0c40165 Compare April 8, 2025 10:30
@illwieckz
Copy link
Member Author

illwieckz commented Apr 8, 2025

Using the same tricks, it is now possible to rebuild sel_ldr for windows-i686 with MinGW.

It means that it is now possible to rebuild the loader for all the DæmonEngine platforms using CMake (including the related nacl_bootstrap_helper when needed), on macOS for the macOS loader, on Linux for all other platforms.

@illwieckz illwieckz force-pushed the illwieckz/cmake branch 2 times, most recently from 8d956b8 to 11e641b Compare April 8, 2025 10:45
@illwieckz
Copy link
Member Author

In fact, only the single file macamd64.inc is enough. One can put it in include-hax/winsdk/ and it works.

@illwieckz
Copy link
Member Author

illwieckz commented Apr 8, 2025

Even better, those two obvious macros are enough:

push_reg macro Reg
	push Reg
	.pushreg Reg
	endm

alloc_stack macro Size
	sub rsp, Size
	.allocstack Size
	endm

So I provided a file named include-hax/fake_masm/macamd64.inc that just contains that, and when using MinGW, JWasm is used with include-hax/fake_masm as include directory.

On MSVC with original MASM, it is meant to use system's macamd64.inc, but this is untested.

@illwieckz
Copy link
Member Author

So this repository can now cross-compile sel_ldr for Windows using MinGW on Linux out of the box.

@illwieckz
Copy link
Member Author

illwieckz commented Apr 8, 2025

I added a commit to statically link the sel_ldr binary, unfortunately it prints this warning:

transport_common.cc:(.text+0x78c): avertissement : Using 'gethostbyname' in statically linked
 applications requires at runtime the shared libraries from the glibc version used for linking

I don't know if that's a problem for us.

@illwieckz illwieckz force-pushed the illwieckz/cmake branch 3 times, most recently from 12459bb to 4340799 Compare December 10, 2025 10:54
@illwieckz
Copy link
Member Author

I removed everything that wasn't CMake, other things like the MinGW compatibility code will be submitted in a separate PR.

@illwieckz
Copy link
Member Author

The CMake code already includes stuff for MinGW:

So this part references files that aren't there yet.

@slipher
Copy link
Member

slipher commented Dec 10, 2025

I removed everything that wasn't CMake, other things like the MinGW compatibility code will be submitted in a separate PR.

Thanks. I will continue to use the Scons build since I am interested in running the tests, among other things. So I won't have to review this super closely since it is only CMake files which can't break the other build.

Are you really using all those Daemon *.cmake files, like Compiler.cmake and SourceGenerator.cmake? I don't see any use of daemon_embed_files for example.

@illwieckz
Copy link
Member Author

Are you really using all those Daemon *.cmake files, like Compiler.cmake and SourceGenerator.cmake? I don't see any use of daemon_embed_files for example.

No, it's just meant to be kept in sync with the made-reusable code from:

@illwieckz illwieckz force-pushed the illwieckz/cmake branch 8 times, most recently from 4ad638b to c725772 Compare December 17, 2025 10:22
@illwieckz illwieckz added the enhancement New feature or request label Dec 17, 2025
@illwieckz illwieckz force-pushed the illwieckz/cmake branch 4 times, most recently from 19d4e54 to 8a51eee Compare December 18, 2025 10:15
@illwieckz illwieckz force-pushed the illwieckz/cmake branch 5 times, most recently from ecaccae to a87e12d Compare December 18, 2025 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants