Skip to content

WIP: Write a CMakeLists.txt to build the NaCl loader #2

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

Draft
wants to merge 21 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
293472f
gitignore: ignore the /build-* folders (/build/ is reserved)
illwieckz Apr 7, 2025
04ead53
cmake: add the DaemonPlatform CMake framework
illwieckz Apr 7, 2025
e50f644
cmake: add MinGW CMake toolchains for cross-compilation
illwieckz Apr 7, 2025
7914498
cmake: add CMake scripts to to build sel_ldr and nacl_helper_bootstrap
illwieckz Apr 7, 2025
0a3e619
win32: do not redefine mode_t with conflicting type on MinGW
illwieckz Apr 8, 2025
4fbabd0
win32: include stdint for INT64_MAX
illwieckz Apr 8, 2025
5f45c90
win32: use lowercase header file name
illwieckz Apr 8, 2025
91ea710
win32: use __inline__ instead of __forceinline__ on MinGW
illwieckz Apr 8, 2025
8ca250e
win32: do not use non-standard i64 integer suffix on MinGW
illwieckz Apr 8, 2025
9c8de16
win32: do not declare PosixSignals enum on MinGW
illwieckz Apr 8, 2025
ac8e5bb
win32: use GCC noinline on MinGW
illwieckz Apr 8, 2025
0e072cf
win32: Add and use a simplistic macamd64.inc
illwieckz Apr 8, 2025
8ef632f
win32: use asm intrinsics alternative to __halt() on MinGW
illwieckz Apr 8, 2025
b87c9c3
win32: use posix intrinsics instead of MSVC asm code on i686 MinGW
illwieckz Apr 8, 2025
5a40450
win32: use posix code in vcpuid.c instead of windows one on i686 MinG…
illwieckz Apr 8, 2025
da98890
win32: do not use fast asm in port_win.c on i686 MinGW
illwieckz Apr 8, 2025
c7befbf
nacl_helper_bootstrap: let Clang find PATH_MAX
illwieckz Apr 8, 2025
2e55e62
android: use standard __ANDROID__ definition
illwieckz Apr 9, 2025
14df547
android: properly detect the GNU version of strerror_r()
illwieckz Apr 9, 2025
62710c6
nacl_helper_bootstrap: also accept statically build sel_ldr
illwieckz Apr 10, 2025
961bfa0
cmake: make possible to build sel_ldr with 16K PageSize
illwieckz Apr 10, 2025
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 @@ -24,6 +24,7 @@ testserver.log
/toolchain_build/src/

# Ignore these absolute directories (relative to native_client directory)
/build-*/
/build/Debug/
/build/Release/
/chromebinaries/
Expand Down
179 changes: 179 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
cmake_minimum_required(VERSION 3.12)

project(native_client C CXX ASM)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

include(DaemonPlatform/Platform)
include(NaClFlags)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE RelWithDebInfo)
endif()

option(BUILD_NACL_LOADER "Build the sel_ldr program." ON)

if (LINUX)
option(BUILD_NACL_HELPER_BOOTSTRAP "Build the nacl_helper_bootstrap program on platforms requiring it." ON)
endif()

# TODO(arbenson): remove this once binutils bug is fixed (see
# src/trusted/service_runtime/arch/x86_64/sel_addrspace_posix_x86_64.c)
if (BUILD_NACL_LOADER AND ARCH_amd64 AND NOT WIN32)
option(USE_AMD64_ZERO_BASED_SANDBOX "Allow the zero-based sandbox model to run insecurely." OFF)
list(APPEND INHERITED_OPTIONS "USE_AMD64_ZERO_BASED_SANDBOX")
endif()

if (BUILD_NACL_LOADER AND WIN32)
option(FORCE_NO_TRUSTED_BUILD "Prevent use of trusted toolchain." OFF)
endif()

if (BUILD_NACL_LOADER AND WIN32)
set(REQUIRE_MASM ON)
endif()

if (BUILD_NACL_LOADER AND APPLE)
set(REQUIRE_PYTHON ON)
endif()

if (BUILD_NACL_HELPER_BOOTSTRAP)
set(REQUIRE_PYTHON ON)
endif()

if (REQUIRE_PYTHON)
if (NOT PYTHON)
find_program(PYTHON NAMES "python3" DOC "Path to the python3 executable." REQUIRED)
endif()
endif()

if (REQUIRE_MASM)
if (NOT MSVC AND NOT CMAKE_ASM_MASM_COMPILER)
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include-hax/fake_masm")

find_program(JWASM NAMES "jwasm" DOC "Path to the JWasm executable." REQUIRED)

set(CMAKE_ASM_MASM_COMPILER "${JWASM}")

if (ARCH_i686)
list(APPEND CMAKE_ASM_MASM_FLAGS "-coff")
elseif(ARCH_amd64)
list(APPEND CMAKE_ASM_MASM_FLAGS "-win64")
endif()
endif()

enable_language(ASM_MASM)
endif()

include_directories("include-hax")

if (ARCH_i686)
set(ARCH_SUFFIX "_x86_32")
elseif (ARCH_amd64)
set(ARCH_SUFFIX "_X86_64")
elseif (ARCH_armhf OR ARCH_armel)
set(ARCH_SUFFIX "_arm")
elseif (ARCH_mipsel)
set(ARCH_SUFFIX "_mips32")
endif()

if (BUILD_NACL_LOADER)
add_subdirectory(src/shared/gio)
add_subdirectory(src/shared/imc)
add_subdirectory(src/shared/platform)
add_subdirectory(src/trusted/cpu_features)
add_subdirectory(src/trusted/debug_stub)
add_subdirectory(src/trusted/desc)
add_subdirectory(src/trusted/fault_injection)
add_subdirectory(src/trusted/interval_multiset)
add_subdirectory(src/trusted/nacl_base)
add_subdirectory(src/trusted/perf_counter)
add_subdirectory(src/trusted/platform_qualify)
add_subdirectory(src/trusted/validator)

if (ARCH_i686 OR ARCH_amd64)
add_subdirectory(src/trusted/validator_x86)
add_subdirectory(src/trusted/validator_ragel)
elseif (ARCH_armhf OR ARCH_armel)
add_subdirectory(src/trusted/validator_arm)
elseif (ARCH_mipsel)
add_subdirectory(src/trusted/validator_mips)
endif()
endif()

if (BUILD_NACL_LOADER)
add_subdirectory(src/trusted/service_runtime)
endif()

if (BUILD_NACL_HELPER_BOOTSTRAP)
option(BUILD_NACL_HELPER_BOOTSTRAP_WITH_CLANG "Build the nacl_helper_bootstrap program with Clang." ON)
endif()

if (BUILD_NACL_HELPER_BOOTSTRAP)
if (DAEMON_C_COMPILER_Clang_COMPATIBILITY)
set(BUILD_BOOTSTRAP_DIRECTLY ON)
elseif (NOT BUILD_NACL_HELPER_BOOTSTRAP_WITH_CLANG)
set(BUILD_BOOTSTRAP_DIRECTLY ON)
endif()

if (BUILD_BOOTSTRAP_DIRECTLY)
add_subdirectory(src/trusted/service_runtime/linux)
else()
if (CMAKE_GENERATOR MATCHES "Visual Studio")
set(BOOTSTRAP_GENERATOR "NMake Makefiles")
else()
set(BOOTSTRAP_GENERATOR "${CMAKE_GENERATOR}")
endif()

find_program(CLANG_C_COMPILER NAMES "clang" DOC "Path to the Clang C compiler." REQUIRED)
find_program(CLANG_CXX_COMPILER NAMES "clang++" DOC "Path to the Clang C++ compiler." REQUIRED)

if (NOT CLANG_TARGET)
execute_process(COMMAND "${CMAKE_CXX_COMPILER}" -dumpmachine
OUTPUT_VARIABLE CLANG_TARGET
OUTPUT_STRIP_TRAILING_WHITESPACE
# CMake 3.18.4 from Debian Buster doesn't support that option:
# COMMAND_ERROR_IS_FATAL ANY
)

if (NOT CLANG_TARGET)
message(FATAL_ERROR "Failed to read the compiler target.")
endif()
endif()

foreach(inherited_option ${INHERITED_OPTIONS})
list(APPEND INHERITED_OPTION_ARGS "-D${inherited_option}=${${inherited_option}}")
endforeach()

set(BOOTSTRAP_BUILD_DIR "${CMAKE_CURRENT_BINARY_DIR}/nacl_helper_bootstrap-build")

include(ExternalProject)

ExternalProject_Add(nacl_helper_bootstrap-project
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
BINARY_DIR "${BOOTSTRAP_BUILD_DIR}"
CMAKE_GENERATOR "${BOOTSTRAP_GENERATOR}"
CMAKE_ARGS
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
"-DCMAKE_C_COMPILER=${CLANG_C_COMPILER}"
"-DCMAKE_CXX_COMPILER=${CLANG_CXX_COMPILER}"
"-DCMAKE_C_FLAGS=--target=${CLANG_TARGET}"
"-DCMAKE_CXX_FLAGS=--target=${CLANG_TARGET}"
-DBUILD_NACL_LOADER=OFF
-DBUILD_NACL_HELPER_BOOTSTRAP=ON
${INHERITED_OPTION_ARGS}
# CMake 3.18.4 from Debian Buster doesn't support that option:
# INSTALL_BYPRODUCTS
INSTALL_COMMAND echo
)

add_custom_target(nacl_helper_bootstrap ALL
COMMAND "${CMAKE_COMMAND}" -E copy "${BOOTSTRAP_BUILD_DIR}/nacl_helper_bootstrap"
"${CMAKE_CURRENT_BINARY_DIR}/nacl_helper_bootstrap"
BYPRODUCTS
"${CMAKE_CURRENT_BINARY_DIR}/nacl_helper_bootstrap"
DEPENDS nacl_helper_bootstrap-project
)
endif()
endif()
121 changes: 121 additions & 0 deletions cmake/DaemonPlatform/Architecture.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Daemon BSD Source Code
# Copyright (c) 2022, Daemon Developers
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the <organization> nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

################################################################################
# Determine architecture
################################################################################

# When adding a new architecture, look at all the places ARCH is used

try_compile(BUILD_RESULT
"${CMAKE_BINARY_DIR}"
"${CMAKE_CURRENT_LIST_DIR}/Architecture/Architecture.cpp"
CMAKE_FLAGS CMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES}
OUTPUT_VARIABLE BUILD_LOG
)

# Setting -Werror in CXXFLAGS would produce errors instead of warning
# but that should not break the architecture detection,
# so we only print a CMake warning there and use BUILD_LOG content to
# detect unsupported platforms.
# Catching compilation error is still useful, for example to detect
# undefined types, missing header or things like that.
# Setting USE_WERROR to ON doesn't print this warning.
if (NOT BUILD_RESULT)
message(WARNING
"Failed to build Architecture.cpp\n"
"Setting -Werror in CXXFLAGS can produce false positive errors\n"
"${BUILD_LOG}"
)
endif()

string(REGEX MATCH "DAEMON_ARCH_([a-zA-Z0-9_]+)" ARCH_DEFINE "${BUILD_LOG}")
string(REPLACE "DAEMON_ARCH_" "" ARCH "${ARCH_DEFINE}")

if (NOT ARCH)
message(FATAL_ERROR
"Missing DAEMON_ARCH, there is a mistake in Architecture.cpp\n"
"${BUILD_LOG}"
)
else()
set(ARCH_${ARCH} ON)

if(ARCH_unsupported)
message(FATAL_ERROR "Architecture not supported")
endif()
endif()

message(STATUS "Detected architecture: ${ARCH}")

add_definitions(-D${ARCH_DEFINE})

# This string can be modified without breaking compatibility.
daemon_add_buildinfo("char*" "DAEMON_ARCH_STRING" "\"${ARCH}\"")

# Modifying NACL_ARCH breaks engine compatibility with nexe game binaries
# since NACL_ARCH contributes to the nexe file name.
set(NACL_ARCH "${ARCH}")
if (LINUX OR FREEBSD)
set(ARMHF_USAGE arm64 armel)
if (ARCH IN_LIST ARMHF_USAGE)
# Load 32-bit armhf nexe on 64-bit arm64 engine on Linux with multiarch.
# The nexe is system agnostic so there should be no difference with armel.
set(NACL_ARCH "armhf")
endif()
elseif(APPLE)
if ("${ARCH}" STREQUAL arm64)
# You can get emulated NaCl going like this:
# cp external_deps/macos-amd64-default_10/{nacl_loader,irt_core-amd64.nexe} build/
set(NACL_ARCH "amd64")
endif()
endif()

daemon_add_buildinfo("char*" "DAEMON_NACL_ARCH_STRING" "\"${NACL_ARCH}\"")

option(USE_ARCH_INTRINSICS "Enable custom code using intrinsics functions or asm declarations" ON)
mark_as_advanced(USE_ARCH_INTRINSICS)

macro(set_arch_intrinsics name)
if (USE_ARCH_INTRINSICS)
message(STATUS "Enabling ${name} architecture intrinsics")
add_definitions(-DDAEMON_USE_ARCH_INTRINSICS_${name}=1)
else()
message(STATUS "Disabling ${name} architecture intrinsics")
endif()
endmacro()

if (USE_ARCH_INTRINSICS)
add_definitions(-DDAEMON_USE_ARCH_INTRINSICS=1)
endif()

set_arch_intrinsics(${ARCH})

set(amd64_PARENT "i686")
set(arm64_PARENT "armhf")

if (${ARCH}_PARENT)
set_arch_intrinsics(${${ARCH}_PARENT})
endif()
Loading