Skip to content

Commit

Permalink
fix cmake dependency hell
Browse files Browse the repository at this point in the history
  • Loading branch information
Wumpf committed Nov 2, 2023
1 parent 4daea3b commit da4b137
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
24 changes: 12 additions & 12 deletions rerun_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ endif()

# ------------------------------------------------------------------------------
# Setup rerun_c dependency.
add_library(rerun_c STATIC IMPORTED)

# Inside the repo build ourselves, otherwise default to a local `lib` folder.
if(DEFINED RERUN_REPOSITORY)
include(build_rerun_c.cmake)
build_rerun_c(RERUN_C_LIB_DEFAULT)
build_rerun_c(rerun_c)
else()
set(RERUN_C_DEFAULT_LIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/lib/)

Expand All @@ -55,21 +57,19 @@ else()
else()
message(WARNING "Unsupported platform ${RERUN_C_LIB_DEFAULT}, can't find rerun_c library.")
endif()
endif()

set(RERUN_C_LIB ${RERUN_C_LIB_DEFAULT} CACHE PATH "\
Where to find the rerun_c library.\n\
If not specified, a local rerun_c with the current system architecture will be used."
)
set(RERUN_C_LIB ${RERUN_C_LIB_DEFAULT} CACHE PATH "\
Where to find the rerun_c library.\n\
If not specified, a local rerun_c with the current system architecture will be used."
)

if("${RERUN_C_LIB}" STREQUAL "")
message(FATAL_ERROR "RERUN_C_LIB is not set.")
if("${RERUN_C_LIB}" STREQUAL "")
message(FATAL_ERROR "RERUN_C_LIB is not set.")
endif()

set_target_properties(rerun_c PROPERTIES IMPORTED_LOCATION ${RERUN_C_LIB})
endif()

# For cleanliness, create a rerun_c target with the correct dependencies.
# This allows other targets to depend on rerun_c directly if they need to.
add_library(rerun_c STATIC IMPORTED)
set_target_properties(rerun_c PROPERTIES IMPORTED_LOCATION ${RERUN_C_LIB})

if(APPLE)
target_link_libraries(rerun_c INTERFACE "-framework CoreFoundation" "-framework IOKit")
Expand Down
9 changes: 4 additions & 5 deletions rerun_cpp/build_rerun_c.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# This works only within a checkout of the Rerun repository, since the Rerun C rust source
# is not part of the Rerun C++ SDK distribution bundle (instead, pre-built libraries are provided).
function (build_rerun_c OUT_C_LIB)
function (build_rerun_c rerun_c)
if(APPLE)
set(RERUN_C_BUILD_ARTIFACT ${CMAKE_CURRENT_SOURCE_DIR}/../target/release/librerun_c.a)
elseif(UNIX) # if(LINUX) # CMake 3.25
Expand All @@ -22,16 +22,15 @@ function (build_rerun_c OUT_C_LIB)
COMMENT "Building rerun_c from source"
)

# It is necessary to add a custom target that depends on the output of the custom command,
# otherwise, cmake has no rule to build the output.
# In CMake you can't depend on an output file directly. We have to wrap this in a target that rerun_c then depends on.
add_custom_target(rerun_c_build DEPENDS "${RERUN_C_BUILD_ARTIFACT}")
add_dependencies(rerun_c rerun_c_build)
set_target_properties(rerun_c PROPERTIES IMPORTED_LOCATION ${RERUN_C_LIB})

# Put `rerun.h` into the same place where it's on a user's machine and apply CMake variables like version number.
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/../crates/rerun_c/src/rerun.h"
"${CMAKE_CURRENT_SOURCE_DIR}/src/rerun/c/rerun.h"
NEWLINE_STYLE LF # Specify line endings, otherwise CMake wants to change them on Windows.
)

set(${OUT_C_LIB} ${RERUN_C_BUILD_ARTIFACT} PARENT_SCOPE)
endfunction()

0 comments on commit da4b137

Please sign in to comment.