|
| 1 | +cmake_minimum_required(VERSION 3.10) |
| 2 | + |
| 3 | +project(MetaCallPoC VERSION 1.0) |
| 4 | +set(CMAKE_C_STANDARD 99) |
| 5 | +set(CMAKE_CXX_STANDARD 11) |
| 6 | +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) |
| 7 | + |
| 8 | +# Default build type |
| 9 | +if(NOT CMAKE_BUILD_TYPE) |
| 10 | + set(CMAKE_BUILD_TYPE "Debug") |
| 11 | +endif() |
| 12 | + |
| 13 | +# Debug |
| 14 | +if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") |
| 15 | + if(WIN32) |
| 16 | + add_compile_options(/fsanitize=address) |
| 17 | + else() |
| 18 | + add_compile_options(-fsanitize=address) |
| 19 | + add_compile_options(-fsanitize=undefined) |
| 20 | + add_link_options(-fsanitize=address) |
| 21 | + add_link_options(-fsanitize=undefined) |
| 22 | + endif() |
| 23 | +endif() |
| 24 | + |
| 25 | +# Test |
| 26 | +include(CTest) |
| 27 | + |
| 28 | +# Windows exports |
| 29 | +if(WIN32) |
| 30 | + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) |
| 31 | +endif() |
| 32 | + |
| 33 | +# PLTHook |
| 34 | +include(FetchContent) |
| 35 | + |
| 36 | +set(PLTHook_SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/plthook") |
| 37 | + |
| 38 | +FetchContent_Declare(PLTHook |
| 39 | + GIT_REPOSITORY https://github.com/metacall/plthook.git |
| 40 | + GIT_TAG master |
| 41 | + SOURCE_DIR ${PLTHook_SOURCE_DIR} |
| 42 | +) |
| 43 | + |
| 44 | +FetchContent_MakeAvailable(PLTHook) |
| 45 | + |
| 46 | +set(PLTHook_INCLUDE_DIR "${PLTHook_SOURCE_DIR}") |
| 47 | + |
| 48 | +if(APPLE) |
| 49 | + set(PLTHOOK_SOURCE "${PLTHook_SOURCE_DIR}/plthook_osx.c") |
| 50 | +elseif(WIN32 OR MINGW) |
| 51 | + set(PLTHOOK_SOURCE "${PLTHook_SOURCE_DIR}/plthook_win32.c") |
| 52 | +else() |
| 53 | + set(PLTHOOK_SOURCE "${PLTHook_SOURCE_DIR}/plthook_elf.c") |
| 54 | +endif() |
| 55 | + |
| 56 | +# MetaCall Library |
| 57 | +add_library(metacall SHARED source/metacall.c ${PLTHOOK_SOURCE}) |
| 58 | +target_include_directories(metacall PRIVATE ${PLTHook_INCLUDE_DIR}) |
| 59 | +target_link_libraries(metacall ${CMAKE_DL_LIBS}) |
| 60 | + |
| 61 | +# NodeJS library of NodeJS static |
| 62 | +add_library(libnode-static source/libnode.c) |
| 63 | +target_compile_definitions(libnode-static PUBLIC NODE_STATIC=1) # Add definition for compiling as "node-static" |
| 64 | + |
| 65 | +# NodeJS static |
| 66 | +add_executable(node-static source/node-static.c) |
| 67 | +target_link_libraries(node-static libnode-static ${CMAKE_DL_LIBS} metacall) |
| 68 | +set_property(TARGET node-static PROPERTY ENABLE_EXPORTS ON) # Required to export the symbols on the executable |
| 69 | +add_test(NAME node-static |
| 70 | + COMMAND $<TARGET_FILE:node-static> |
| 71 | +) |
| 72 | + |
| 73 | +# NodeJS library of NodeJS dynamic |
| 74 | +add_library(libnode SHARED source/libnode.c) |
| 75 | + |
| 76 | +# NodeJS dynamic |
| 77 | +add_executable(node-dynamic source/node-dynamic.c) |
| 78 | +target_link_libraries(node-dynamic ${CMAKE_DL_LIBS} metacall) |
| 79 | +add_test(NAME node-dynamic |
| 80 | + COMMAND $<TARGET_FILE:node-dynamic> |
| 81 | +) |
| 82 | + |
| 83 | +# Normal Executable |
| 84 | +add_executable(normal-executable source/normal-executable.c) |
| 85 | +target_link_libraries(normal-executable ${CMAKE_DL_LIBS} metacall) |
| 86 | +add_test(NAME normal-executable |
| 87 | + COMMAND $<TARGET_FILE:normal-executable> |
| 88 | +) |
| 89 | +set_property(TEST normal-executable PROPERTY ENVIRONMENT "NORMAL_EXECUTABLE=1") |
| 90 | + |
| 91 | +# NodeJS Loader is linked weakly to libnode2 |
| 92 | +add_library(node_loader SHARED source/node_loader.c) |
| 93 | +target_link_libraries(node_loader |
| 94 | + PRIVATE |
| 95 | + |
| 96 | + # Delay load for MSVC |
| 97 | + $<$<CXX_COMPILER_ID:MSVC>:libnode2> |
| 98 | + $<$<CXX_COMPILER_ID:MSVC>:delayimp> |
| 99 | +) |
| 100 | +target_link_options(node_loader |
| 101 | + PRIVATE |
| 102 | + # TODO: Add flags for gcc? |
| 103 | + $<$<AND:$<BOOL:${APPLE}>,$<CXX_COMPILER_ID:AppleClang,Clang>>:-Wl,-undefined,dynamic_lookup> |
| 104 | + $<$<CXX_COMPILER_ID:MSVC>:/DELAYLOAD:$<TARGET_FILE_BASE_NAME:libnode2>.dll> |
| 105 | +) |
| 106 | + |
| 107 | +# NodeJS library of NodeJS Loader |
| 108 | +add_library(libnode2 SHARED source/libnode2.c source/libnode2.cpp) |
| 109 | + |
| 110 | +# Additional dlinfo test (unrelated to the PoC) |
| 111 | +if(NOT WIN32 AND NOT APPLE) |
| 112 | + add_executable(dlinfo-test source/dlinfo-test.c) |
| 113 | + target_link_libraries(dlinfo-test ${CMAKE_DL_LIBS}) |
| 114 | +endif() |
0 commit comments