Skip to content

Commit

Permalink
Restructure project to use proper CMake cross-compilation
Browse files Browse the repository at this point in the history
If one were to build this project on Linux, it would fail
because CMake would send Linux ld flags to the Darwin ld,
which uses a completely different syntax. This required
breaking out the tools into a separate CMake invocation.

I have verified that the entire project builds from an
empty build folder following these changes.
  • Loading branch information
wjk committed Oct 13, 2023
1 parent a9f762d commit a66e5f7
Show file tree
Hide file tree
Showing 20 changed files with 174 additions and 104 deletions.
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ endif()
cmake_minimum_required(VERSION 3.15.1)
project(PUREDARWIN)

if(NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
message(FATAL_ERROR "CMAKE_SYSTEM_NAME not set properly, are you sure you are using configure.sh?")
endif()

if((NOT CMAKE_C_COMPILER_ID MATCHES "AppleClang") AND (NOT CMAKE_C_COMPILER_ID MATCHES "Clang"))
message(FATAL_ERROR "PureDarwin must be compiled with clang.")
endif()
Expand All @@ -24,6 +28,7 @@ include(cmake/install_helpers.cmake)
include(cmake/kext.cmake)
include(cmake/mig.cmake)

include(cmake/tools.cmake)

add_subdirectory(projects)
add_subdirectory(src)
add_subdirectory(tools)
8 changes: 4 additions & 4 deletions cmake/mig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ function(mig filename)
get_filename_component(basename ${filename} NAME_WE)
get_filename_component(filename_abs ${filename} ABSOLUTE)
add_custom_command(OUTPUT ${MIG_DEPS}
COMMAND ${CMAKE_COMMAND} -E env MIGCOM=$<TARGET_FILE:migcom> ${PUREDARWIN_SOURCE_DIR}/tools/mig/mig.sh -arch ${MIG_ARCH}
-user ${MIG_USER_SOURCE} -header ${MIG_USER_HEADER} -server ${MIG_SERVER_SOURCE}
-sheader ${MIG_SERVER_HEADER} ${MIG_FLAGS} ${filename_abs}
DEPENDS migcom
COMMAND mig -arch ${MIG_ARCH} -migcom $<TARGET_FILE:host_migcom>
-user ${MIG_USER_SOURCE} -header ${MIG_USER_HEADER} -server ${MIG_SERVER_SOURCE}
-sheader ${MIG_SERVER_HEADER} ${MIG_FLAGS} ${filename_abs}
DEPENDS mig host_migcom
COMMENT "MiG ${filename}" VERBATIM COMMAND_EXPAND_LISTS
)
endfunction()
2 changes: 2 additions & 0 deletions cmake/toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(CMAKE_SYSTEM_NAME Darwin)
set(CMAKE_SYSTEM_VERSION 23.0.0)
41 changes: 41 additions & 0 deletions cmake/tools.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
ExternalProject_Add(tools-extproj
SOURCE_DIR ${CMAKE_SOURCE_DIR}/tools
PREFIX ${CMAKE_BINARY_DIR}/tools
BINARY_DIR ${CMAKE_BINARY_DIR}/tools
CMAKE_ARGS
-UCMAKE_TOOLCHAIN_FILE
-DCMAKE_INSTALL_PREFIX=${CMAKE_BINARY_DIR}/tools/bin
-DTOOLS_FOLDER=${CMAKE_BINARY_DIR}/tools/bin
BUILD_ALWAYS TRUE
INSTALL_COMMAND ""
BUILD_BYPRODUCTS ${tools_targets}
USES_TERMINAL_CONFIGURE TRUE USES_TERMINAL_BUILD TRUE
)

ExternalProject_Get_Property(tools-extproj INSTALL_DIR)

macro(add_imported_tool name)
add_executable(host_${name} IMPORTED)
set_property(TARGET host_${name} PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/bin/${name})
add_dependencies(host_${name} tools-extproj)
# message(STATUS "Imported tool ${name}.")
endmacro()

macro(add_imported_tools)
foreach(tool ${ARGN})
add_imported_tool(${tool})
endforeach()
endmacro()

# message(STATUS "About to add imported tools.")
add_imported_tools(
ctfconvert ctfdump ctfmerge xar otool ld64 unifdef migcom
checksyms lipo size strings nm libtool redo_prebinding
seg_addr_table seg_hack install_name_tool indr strip segedit
pagestuff codesign_allocate bitcode_strip ctf_insert check_dylib
cmpdylib inout vtool nmedit ld
)

add_executable(mig IMPORTED)
set_property(TARGET mig PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/tools/mig/mig.sh)
add_dependencies(mig host_migcom)
12 changes: 12 additions & 0 deletions configure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/sh

# Use this script to run the original CMake configuration process.
# It automatically sets what command-line properties are needed.
# You can then freely build and reconfigure from the build directory.

MY_DIR=$(cd `dirname $0` && pwd)
mkdir -p $MY_DIR/build
cd $MY_DIR/build

echo "Building in $(pwd)"
cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchain.cmake ..
6 changes: 3 additions & 3 deletions src/Kernel/Extensions/IOStorageFamily/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ set(HEADERS
)
foreach(file IN LISTS HEADERS)
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/user_include/IOKit/storage/${file}
COMMAND $<TARGET_FILE:unifdef> -UKERNEL -o ${CMAKE_CURRENT_BINARY_DIR}/user_include/IOKit/storage/${file} ${CMAKE_CURRENT_SOURCE_DIR}/include/IOKit/storage/${file}
DEPENDS unifdef ${CMAKE_CURRENT_SOURCE_DIR}/include/IOKit/storage/${file}
COMMENT "unifdef ${file}" VERBATIM
COMMAND $<TARGET_FILE:host_unifdef> -UKERNEL -o ${CMAKE_CURRENT_BINARY_DIR}/user_include/IOKit/storage/${file} ${CMAKE_CURRENT_SOURCE_DIR}/include/IOKit/storage/${file}
DEPENDS host_unifdef ${CMAKE_CURRENT_SOURCE_DIR}/include/IOKit/storage/${file}
DEPENDS host_unifdef COMMENT "unifdef ${file}" VERBATIM
)
target_sources(IOStorageFamilyUserHeaders PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/user_include/IOKit/storage/${file})
endforeach()
Expand Down
16 changes: 8 additions & 8 deletions src/Kernel/xnu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ externalproject_add(xnu_headers.extproj
-D CTFMERGE_PATH=$<TARGET_FILE:host_ctfmerge>
-D CTFINSERT_PATH=$<TARGET_FILE:host_ctf_insert>
-D AVAILABILITY_PL_PATH=$<TARGET_FILE:availability.pl>
-D MIG_PATH=${PUREDARWIN_SOURCE_DIR}/tools/mig/mig.sh
-D MIGCOM_PATH=$<TARGET_FILE:migcom>
-D MIG_PATH=$<TARGET_FILE:mig>
-D MIGCOM_PATH=$<TARGET_FILE:host_migcom>
-D STRIP_PATH=$<TARGET_FILE:host_strip>
-D LIPO_PATH=$<TARGET_FILE:host_lipo>
-D NM_PATH=$<TARGET_FILE:host_nm>
-D NMEDIT_PATH=$<TARGET_FILE:host_nmedit>
-D LIBTOOL_PATH=$<TARGET_FILE:host_libtool>
-D UNIFDEF_PATH=$<TARGET_FILE:unifdef>
-D UNIFDEF_PATH=$<TARGET_FILE:host_unifdef>
-D DSYMUTIL_PATH=${dsymutil_path}
-P ${CMAKE_CURRENT_SOURCE_DIR}/preprocess_files.cmake
COMMAND
Expand All @@ -34,7 +34,7 @@ externalproject_add(xnu_headers.extproj
USES_TERMINAL_BUILD TRUE
)
add_dependencies(xnu_headers.extproj host_codesign_allocate
migcom host_strip host_lipo host_nm unifdef host_nmedit
mig host_strip host_lipo host_nm host_unifdef host_nmedit
host_ctfconvert host_ctfmerge availability.pl)

add_library(xnu_headers INTERFACE)
Expand Down Expand Up @@ -78,14 +78,14 @@ externalproject_add(xnu
-D CTFINSERT_PATH=$<TARGET_FILE:host_ctf_insert>
-D AVAILABILITY_PL_PATH=$<TARGET_FILE:availability.pl>
-D LD_PATH=$<TARGET_FILE:host_ld>
-D MIG_PATH=${PUREDARWIN_SOURCE_DIR}/tools/mig/mig.sh
-D MIGCOM_PATH=$<TARGET_FILE:migcom>
-D MIG_PATH=$<TARGET_FILE:mig>
-D MIGCOM_PATH=$<TARGET_FILE:host_migcom>
-D STRIP_PATH=$<TARGET_FILE:host_strip>
-D LIPO_PATH=$<TARGET_FILE:host_lipo>
-D NM_PATH=$<TARGET_FILE:host_nm>
-D NMEDIT_PATH=$<TARGET_FILE:host_nmedit>
-D LIBTOOL_PATH=$<TARGET_FILE:host_libtool>
-D UNIFDEF_PATH=$<TARGET_FILE:unifdef>
-D UNIFDEF_PATH=$<TARGET_FILE:host_unifdef>
-D DSYMUTIL_PATH=${dsymutil_path}
-D FIREHOSE_KERNEL_LIBRARY_PATH=$<TARGET_FILE:libfirehose_kernel>
-D FIREHOSE_KERNEL_HEADER_PATH=$<TARGET_PROPERTY:libfirehose_kernel,INTERFACE_INCLUDE_DIRECTORIES>
Expand All @@ -100,7 +100,7 @@ externalproject_add(xnu
USES_TERMINAL_CONFIGURE TRUE
USES_TERMINAL_BUILD TRUE
)
add_dependencies(xnu xnu_headers libfirehose_kernel host_codesign_allocate host_ctfconvert host_ctfmerge migcom host_strip host_lipo host_nm unifdef host_nmedit availability.pl host_ld)
add_dependencies(xnu xnu_headers libfirehose_kernel host_codesign_allocate host_ctfconvert host_ctfmerge host_migcom mig host_lipo host_nm host_unifdef host_nmedit availability.pl host_ld)

install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/xnu/System/Library/Frameworks/Kernel.framework/Versions/A/Resources DESTINATION System/Library/Frameworks/Kernel.framework/Versions/A COMPONENT BaseSystem)
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/xnu/System/Library/Frameworks/Kernel.framework/Versions/A/Headers DESTINATION System/Library/Frameworks/Kernel.framework/Versions/A COMPONENT DeveloperTools)
Expand Down
12 changes: 12 additions & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
cmake_minimum_required(VERSION 3.15.1)
project(PUREDARWIN_TOOLS)

function(add_host_tool name)
add_executable(${name})
install(TARGETS ${name})

if(TOOLS_FOLDER)
set_target_properties(${name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${TOOLS_FOLDER})
endif()
endfunction()

add_subdirectory(cctools)
add_subdirectory(dtrace_ctf)
add_subdirectory(mig)
Expand Down
14 changes: 6 additions & 8 deletions tools/cctools/ld64/src/3rd/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_library(host_ld_helper STATIC)
target_sources(host_ld_helper PRIVATE
add_library(ld_helper STATIC)
target_sources(ld_helper PRIVATE
helper.c
qsort_r.c
strlcat.c
Expand All @@ -9,13 +9,11 @@ target_sources(host_ld_helper PRIVATE
mkpath_np.c
find_executable.c
)
set_property(TARGET host_ld_helper PROPERTY OUTPUT_NAME ld_helper)
target_include_directories(host_ld_helper PRIVATE include ../../../include ../../../include/foreign)
target_compile_definitions(host_ld_helper PRIVATE -D__DARWIN_UNIX03)
target_include_directories(ld_helper PRIVATE include ../../../include ../../../include/foreign)
target_compile_definitions(ld_helper PRIVATE -D__DARWIN_UNIX03)

add_library(host_ld_BlocksRuntime STATIC)
set_property(TARGET host_ld_BlocksRuntime PROPERTY OUTPUT_NAME ld_BlocksRuntime)
target_sources(host_ld_BlocksRuntime PRIVATE
add_library(ld_BlocksRuntime STATIC)
target_sources(ld_BlocksRuntime PRIVATE
BlocksRuntime/data.c
BlocksRuntime/runtime.c
)
18 changes: 9 additions & 9 deletions tools/cctools/ld64/src/ld/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ add_subdirectory(passes)

find_package(OpenSSL REQUIRED)

add_executable(host_ld)
target_sources(host_ld PRIVATE
add_host_tool(ld)
target_sources(ld PRIVATE
debugline.c
InputFiles.cpp
ld.cpp
Expand All @@ -17,15 +17,15 @@ target_sources(host_ld PRIVATE
code-sign-blobs/blob.cpp
)

target_compile_definitions(host_ld PRIVATE __DARWIN_UNIX03)
target_compile_options(host_ld PRIVATE "-Wno-#warnings" -Wno-deprecated)
target_link_libraries(host_ld PRIVATE
host_libxar host_ld_helper host_ld_BlocksRuntime host_ld_libparsers host_ld_libpasses host_libstuff
target_compile_definitions(ld PRIVATE __DARWIN_UNIX03)
target_compile_options(ld PRIVATE "-Wno-#warnings" -Wno-deprecated)
target_link_libraries(ld PRIVATE
libxar ld_helper ld_BlocksRuntime ld_libparsers ld_libpasses libstuff
OpenSSL::SSL OpenSSL::Crypto
)
set_property(TARGET host_ld PROPERTY OUTPUT_NAME ld)
set_property(TARGET host_ld PROPERTY CXX_STANDARD 11)
target_include_directories(host_ld PRIVATE
set_property(TARGET ld PROPERTY OUTPUT_NAME ld)
set_property(TARGET ld PROPERTY CXX_STANDARD 11)
target_include_directories(ld PRIVATE
../../../include
../../../include/foreign
..
Expand Down
13 changes: 7 additions & 6 deletions tools/cctools/ld64/src/ld/parsers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_library(host_ld_libparsers STATIC)
target_sources(host_ld_libparsers PRIVATE
add_library(ld_libparsers STATIC)
target_sources(ld_libparsers PRIVATE
archive_file.cpp
lto_file.cpp
macho_dylib_file.cpp
Expand All @@ -8,8 +8,9 @@ target_sources(host_ld_libparsers PRIVATE
textstub_dylib_file.cpp
)

target_compile_definitions(host_ld_libparsers PRIVATE __DARWIN_UNIX03)
target_include_directories(host_ld_libparsers PRIVATE
set_property(TARGET ld_libparsers PROPERTY OUTPUT_NAME ld_parsers)
target_compile_definitions(ld_libparsers PRIVATE __DARWIN_UNIX03)
target_include_directories(ld_libparsers PRIVATE
../../../../include
../../../../include/foreign
../../abstraction
Expand All @@ -18,5 +19,5 @@ target_include_directories(host_ld_libparsers PRIVATE
../..
..
)
target_link_libraries(host_ld_libparsers PRIVATE host_ld_helper)
set_property(TARGET host_ld_libparsers PROPERTY CXX_STANDARD 11)
target_link_libraries(ld_libparsers PRIVATE ld_helper)
set_property(TARGET ld_libparsers PROPERTY CXX_STANDARD 11)
11 changes: 6 additions & 5 deletions tools/cctools/ld64/src/ld/passes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_library(host_ld_libpasses STATIC)
target_sources(host_ld_libpasses PRIVATE
add_library(ld_libpasses STATIC)
target_sources(ld_libpasses PRIVATE
branch_island.cpp
branch_shim.cpp
compact_unwind.cpp
Expand All @@ -17,8 +17,9 @@ target_sources(host_ld_libpasses PRIVATE
inits.cpp
)

target_compile_definitions(host_ld_libpasses PRIVATE __DARWIN_UNIX03)
target_include_directories(host_ld_libpasses PRIVATE
set_property(TARGET ld_libpasses PROPERTY OUTPUT_NAME ld_passes)
target_compile_definitions(ld_libpasses PRIVATE __DARWIN_UNIX03)
target_include_directories(ld_libpasses PRIVATE
../../../../include
../../../../include/foreign
../../abstraction
Expand All @@ -28,4 +29,4 @@ target_include_directories(host_ld_libpasses PRIVATE
../../3rd
../../..
)
set_property(TARGET host_ld_libpasses PROPERTY CXX_STANDARD 11)
set_property(TARGET ld_libpasses PROPERTY CXX_STANDARD 11)
8 changes: 4 additions & 4 deletions tools/cctools/libmacho/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
add_library(host_libmacho STATIC)
target_sources(host_libmacho PRIVATE
add_library(libmacho STATIC)
target_sources(libmacho PRIVATE
arch.c
)
set_property(TARGET host_libmacho PROPERTY OUTPUT_NAME macho)
target_include_directories(host_libmacho PRIVATE ../include ../include/foreign)
set_property(TARGET libmacho PROPERTY OUTPUT_NAME macho)
target_include_directories(libmacho PRIVATE ../include ../include/foreign)
10 changes: 5 additions & 5 deletions tools/cctools/libstuff/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
add_library(host_libstuff STATIC)
target_sources(host_libstuff PRIVATE
add_library(libstuff STATIC)
target_sources(libstuff PRIVATE
allocate.c
apple_version.c
arch_usage.c
Expand Down Expand Up @@ -44,12 +44,12 @@ target_sources(host_libstuff PRIVATE
writeout.c
)

set_property(TARGET host_libstuff PROPERTY OUTPUT_NAME stuff)
target_compile_definitions(host_libstuff PRIVATE
set_property(TARGET libstuff PROPERTY OUTPUT_NAME stuff)
target_compile_definitions(libstuff PRIVATE
__DARWIN_UNIX03
PROGRAM_PREFIX=\"\"
)
target_include_directories(host_libstuff PRIVATE
target_include_directories(libstuff PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/../include
${CMAKE_CURRENT_SOURCE_DIR}/../include/foreign
)
30 changes: 16 additions & 14 deletions tools/cctools/misc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
macro(cctools_misc_executable name)
add_executable(host_${name} ${name}.c)
set_property(TARGET host_${name} PROPERTY OUTPUT_NAME ${name})
target_link_libraries(host_${name} PRIVATE host_libmacho host_libstuff)
target_include_directories(host_${name} PRIVATE ../include ../include/foreign ../libstuff)
target_compile_definitions(host_${name} PRIVATE -D__DARWIN_UNIX03)
add_host_tool(${name})
target_sources(${name} PRIVATE ${name}.c)
target_link_libraries(${name} PRIVATE libmacho libstuff)
target_include_directories(${name} PRIVATE ../include ../include/foreign ../libstuff)
target_compile_definitions(${name} PRIVATE -D__DARWIN_UNIX03)
endmacro()

cctools_misc_executable(checksyms)
Expand All @@ -22,20 +22,22 @@ cctools_misc_executable(segedit)
cctools_misc_executable(pagestuff)
cctools_misc_executable(codesign_allocate)
cctools_misc_executable(bitcode_strip)
target_compile_definitions(host_bitcode_strip PRIVATE -DALLOW_ARCHIVES)
target_compile_definitions(bitcode_strip PRIVATE -DALLOW_ARCHIVES)
cctools_misc_executable(ctf_insert)
cctools_misc_executable(check_dylib)
cctools_misc_executable(cmpdylib)
cctools_misc_executable(inout)
cctools_misc_executable(vtool)

# Special cases
add_executable(host_nmedit strip.c)
target_link_libraries(host_nmedit PRIVATE host_libmacho host_libstuff)
target_include_directories(host_nmedit PRIVATE ../include ../include/foreign ../libstuff)
target_compile_definitions(host_nmedit PRIVATE -D__DARWIN_UNIX03 -DNMEDIT)
add_host_tool(nmedit)
target_sources(nmedit PRIVATE strip.c)
target_link_libraries(nmedit PRIVATE libmacho libstuff)
target_include_directories(nmedit PRIVATE ../include ../include/foreign ../libstuff)
target_compile_definitions(nmedit PRIVATE -D__DARWIN_UNIX03 -DNMEDIT)

add_executable(host_ranlib libtool.c)
target_link_libraries(host_ranlib PRIVATE host_libmacho host_libstuff)
target_include_directories(host_ranlib PRIVATE ../include ../include/foreign ../libstuff)
target_compile_definitions(host_ranlib PRIVATE -D__DARWIN_UNIX03 -DRANLIB)
add_host_tool(ranlib)
target_sources(ranlib PRIVATE libtool.c)
target_link_libraries(ranlib PRIVATE libmacho libstuff)
target_include_directories(ranlib PRIVATE ../include ../include/foreign ../libstuff)
target_compile_definitions(ranlib PRIVATE -D__DARWIN_UNIX03 -DRANLIB)
Loading

0 comments on commit a66e5f7

Please sign in to comment.