~cypheon/rapid

f3b86d98e777a2574f6653abcfd830a5c7c3a149 — Johann Rudloff 3 months ago 8dad4f5
Compile RTS to a normal static library instead of bitcode archve
3 files changed, 14 insertions(+), 47 deletions(-)

M CMakeLists.txt
M rts/CMakeLists.txt
M src/Compiler/Codegen/LLVM.idr
M CMakeLists.txt => CMakeLists.txt +2 -2
@@ 1,6 1,6 @@
cmake_minimum_required(VERSION 3.20)

project(rapid)
project(rapid C ASM)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")



@@ 35,7 35,7 @@ add_subdirectory(rts)

add_custom_target(rapidc_test
  COMMAND cmake -E env "IDRIS2_DATA=${RAPID_SUPPORT_BINARIES}:${CMAKE_CURRENT_SOURCE_DIR}/support:$ENV{IDRIS2_DATA}" rapidc=${rapidc_EXECUTABLE} ./runtests.sh --good
  DEPENDS rapidc rapid_rts
  DEPENDS rapidc runtime
  WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
  USES_TERMINAL
  )

M rts/CMakeLists.txt => rts/CMakeLists.txt +8 -39
@@ 9,9 9,7 @@ set(RTS_SOURCES
  vendor/hashset.c
  )

set(RTS_BITCODE)
set(RAPID_RTS_LIB "${RAPID_SUPPORT_BINARIES}/rapid/runtime.bc")
set(RAPID_PLATFORM_LIB "${RAPID_SUPPORT_BINARIES}/rapid/platform.a")
set(RTS_PLATFORM_SOURCE "arch_x86-64.S")

set(RTS_INCLUDES
  "."


@@ 36,41 34,12 @@ if (RAPID_UBSAN_ENABLED)
  list(APPEND RTS_CFLAGS "-fsanitize=undefined")
endif()

foreach(FILE ${RTS_INCLUDES})
  list(APPEND RTS_INCLUDE_FLAGS "-I" "${FILE}")
endforeach()

foreach(C_SOURCE ${RTS_SOURCES} "entry.c")
  string(REGEX REPLACE "\.c$" ".bc" BC_TARGET ${C_SOURCE})
  add_custom_command(
    OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${BC_TARGET}"
    COMMAND "${CLANG_EXECUTABLE}" ARGS "-flto" "-g" "-O3" "-c" "-MMD" "-MF${CMAKE_CURRENT_BINARY_DIR}/${BC_TARGET}.d"
    ${RTS_CFLAGS} ${RTS_INCLUDE_FLAGS} "-std=c11" "-o" "${CMAKE_CURRENT_BINARY_DIR}/${BC_TARGET}" "${CMAKE_CURRENT_SOURCE_DIR}/${C_SOURCE}"
    DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${C_SOURCE}"
    DEPFILE "${CMAKE_CURRENT_BINARY_DIR}/${BC_TARGET}.d"
    )
  set(RTS_BITCODE "${RTS_BITCODE};${CMAKE_CURRENT_BINARY_DIR}/${BC_TARGET}")
endforeach()

add_custom_command(
  OUTPUT "${RAPID_RTS_LIB}"
  COMMAND llvm-link ARGS -o "${RAPID_RTS_LIB}"
  ${RTS_BITCODE}
  DEPENDS ${RTS_BITCODE}
  )

set(RAPID_PLATFORM_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/arch_x86-64.S")
add_custom_command(
  OUTPUT "${RAPID_PLATFORM_LIB}"
  COMMAND "${CLANG_EXECUTABLE}" ARGS "-c" "-o" "${RAPID_PLATFORM_LIB}.o" ${RAPID_PLATFORM_SOURCE}
  COMMAND llvm-ar ARGS rcsu "${RAPID_PLATFORM_LIB}" "${RAPID_PLATFORM_LIB}.o"
  DEPENDS ${RAPID_PLATFORM_SOURCE}
  BYPRODUCTS "${RAPID_PLATFORM_LIB}.o"
  )

add_custom_target(rapid_rts ALL
  DEPENDS "${RAPID_RTS_LIB}" "${RAPID_PLATFORM_LIB}"
  )
add_library(runtime STATIC ${RTS_SOURCES} ${RTS_PLATFORM_SOURCE} "entry.c")
target_compile_options(runtime PUBLIC "-g" "-O2" "-std=c11" ${RTS_CFLAGS})
set_target_properties(runtime PROPERTIES
  ARCHIVE_OUTPUT_DIRECTORY "${RAPID_SUPPORT_BINARIES}/rapid"
  PREFIX "")
target_include_directories(runtime PUBLIC ${RTS_INCLUDES})

#######
# tests


@@ 84,6 53,7 @@ add_executable(rapid_rts_test
  test/test_gc.c
  test/test_memory.c
  ${TEST_SOURCES}
  ${RTS_PLATFORM_SOURCE}
  )
target_compile_options(rapid_rts_test PUBLIC -g ${RTS_CFLAGS}
  "-fsanitize=undefined" "-fsanitize=address")


@@ 100,7 70,6 @@ target_link_libraries(rapid_rts_test PUBLIC
  gc
  gmp
  m
  "${RAPID_PLATFORM_LIB}"
  )

add_custom_target(test_builtin COMMAND rapid_rts_test builtin)

M src/Compiler/Codegen/LLVM.idr => src/Compiler/Codegen/LLVM.idr +4 -6
@@ 126,8 126,7 @@ compileExprWhole c s tmpDir outputDir term outfile = do

  -- load supporting files first, so we can fail early
  support <- readDataFile $ "rapid" </> "support.ll"
  runtime <- findDataFile $ "rapid" </> "runtime.bc"
  platformLib <- findDataFile $ "rapid" </> "platform.a"
  runtime <- findDataFile $ "rapid" </> "runtime.a"

  cd <- logTime 0 "Get CompileData" $ getCompileData False Cases term
  let mainExpr = forget (cd.mainExpr)


@@ 136,7 135,7 @@ compileExprWhole c s tmpDir outputDir term outfile = do

  let preamble = support ++ makeEnterFunction mainName ++ gcPreamble opts
  compileFunctions allFunctionsRaw preamble objectFileName opts
  linkExecutable opts binaryFileName [runtime, platformLib, objectFileName]
  linkExecutable opts binaryFileName [runtime, objectFileName]

  pure $ Nothing



@@ 165,8 164,7 @@ compileExprInc c s tmpDir outputDir term outfile = do

  -- load supporting files first, so we can fail early
  support <- readDataFile $ "rapid" </> "support.ll"
  runtime <- findDataFile $ "rapid" </> "runtime.bc"
  platformLib <- findDataFile $ "rapid" </> "platform.a"
  runtime <- findDataFile $ "rapid" </> "runtime.a"
  opts <- parseDirectives Incremental

  defs <- get Ctxt


@@ 184,7 182,7 @@ compileExprInc c s tmpDir outputDir term outfile = do
  let preamble = support ++ makeEnterFunction mainName ++ gcPreamble opts
  compileFunctions allFunctionsRaw preamble objectFileName opts

  linkExecutable opts binaryFileName ([runtime, platformLib, objectFileName] ++ modFiles)
  linkExecutable opts binaryFileName ([runtime, objectFileName] ++ modFiles)

  pure $ Nothing