Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
Modify CMake files to allow the library be included to other projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Harrm committed May 22, 2019
1 parent 2af37ab commit 4918290
Showing 1 changed file with 41 additions and 50 deletions.
91 changes: 41 additions & 50 deletions rust.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
must_have_exe(rustc)
must_have_exe(cargo)
must_have_exe(cbindgen)


if (CMAKE_BUILD_TYPE STREQUAL "Release")
set(path_prefix "${CMAKE_BINARY_DIR}/release")
Expand All @@ -20,7 +18,7 @@ endif ()
message(STATUS "[sr25519] library: ${lib}")


set(include_path ${CMAKE_BINARY_DIR}/include)
set(include_path ${CMAKE_CURRENT_SOURCE_DIR}/include)
set(sr25519_h_dir ${include_path}/sr25519)


Expand All @@ -47,60 +45,53 @@ ifd_install(${lib} ${CMAKE_INSTALL_LIBDIR})


### setup tasks
add_custom_target(
cargo_build
ALL
execute_process(
COMMAND cargo build --target-dir ${CMAKE_BINARY_DIR} ${release_option}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

add_custom_target(
bindings
ALL
must_have_exe(cbindgen)

execute_process(
COMMAND cbindgen -o ${sr25519_h_dir}/sr25519.h
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)


if (TESTING)
### create imported target for tests
add_library(sr25519 UNKNOWN IMPORTED)

# if we build static lib
if (NOT BUILD_SHARED_LIBS)
if (APPLE)
# on apple we need to link Security
find_library(Security Security)
find_package_handle_standard_args(sr25519
REQUIRED_VARS Security
)
set_target_properties(sr25519 PROPERTIES
INTERFACE_LINK_LIBRARIES ${Security}
)
elseif (UNIX)
# on Linux we need to link pthread
target_link_libraries(sr25519 INTERFACE
pthread
-Wl,--no-as-needed
dl
)
else ()
message(WARNING "You're building static lib, it may not link. Come here and fix.")
endif ()
add_library(sr25519 UNKNOWN IMPORTED GLOBAL)

# if we build static lib
if (NOT BUILD_SHARED_LIBS)
if (APPLE)
# on apple we need to link Security
find_library(Security Security)
find_package_handle_standard_args(sr25519
REQUIRED_VARS Security
)
set_target_properties(sr25519 PROPERTIES
INTERFACE_LINK_LIBRARIES ${Security}
)
elseif (UNIX)
# on Linux we need to link pthread
target_link_libraries(sr25519 INTERFACE
pthread
-Wl,--no-as-needed
dl
)
else ()
message(WARNING "You're building static lib, it may not link. Come here and fix.")
endif ()
endif ()

set_target_properties(sr25519 PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${include_path}
IMPORTED_LOCATION ${lib}
)
add_dependencies(sr25519 bindings cargo_build)
file(MAKE_DIRECTORY ${sr25519_h_dir})
set_target_properties(sr25519 PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${include_path}
IMPORTED_LOCATION ${lib}
)
file(MAKE_DIRECTORY ${sr25519_h_dir})


### add tests
add_test(
NAME cargo_test
COMMAND cargo test
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)
endif ()
### add tests
add_test(
NAME cargo_test
COMMAND cargo test
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)

0 comments on commit 4918290

Please sign in to comment.