Skip to content

Commit

Permalink
Cmake (#3)
Browse files Browse the repository at this point in the history
* Create CMake project

* Add cmake test

* Misc cleanup
  • Loading branch information
zlstringham authored Mar 30, 2022
1 parent c476e43 commit 8837bd8
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.12.4)

project(byondrs VERSION 0.1.0)

if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
include(CTest)
endif ()

add_subdirectory(c-ffi)
70 changes: 70 additions & 0 deletions c-ffi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
find_program(CARGO cargo REQUIRED)

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CARGO_CMD ${CARGO} build)
else ()
set(CARGO_CMD ${CARGO} build --release)
endif ()

set(BYONDRS_INCLUDE ${CMAKE_CURRENT_BINARY_DIR}/include)
set(BYONDRS_H ${BYONDRS_INCLUDE}/byondrs/byondrs.h)

add_custom_target(
byondrs_rust ALL
COMMAND ${CMAKE_COMMAND} -E env CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR}
${CARGO_CMD}
BYPRODUCTS ${BYONDRS_H}
COMMENT "Building byondrs rust library."
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

add_library(byondrs SHARED IMPORTED GLOBAL)
add_dependencies(byondrs byondrs_rust)

# Hack: target_include_directories() fails at configure time if the directory
# doesn't exist, but the custom target generates it at build time.
file(MAKE_DIRECTORY ${BYONDRS_INCLUDE})
target_include_directories(byondrs INTERFACE ${BYONDRS_INCLUDE})

# Hack: target_sources() fails at configure time if the file doesn't exist, but
# the custom target generates it at build time.
file(MAKE_DIRECTORY ${BYONDRS_INCLUDE}/byondrs)
file(TOUCH ${BYONDRS_H})
target_sources(byondrs INTERFACE ${BYONDRS_H})

# TODO: Verify this works on Windows.
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
set_property(
TARGET byondrs PROPERTY IMPORTED_LOCATION
${CMAKE_CURRENT_BINARY_DIR}/release/byondrs.dll)
set_property(
TARGET byondrs PROPERTY IMPORTED_IMPLIB
${CMAKE_CURRENT_BINARY_DIR}/release/byondrs.dll.lib)
set_property(
TARGET byondrs PROPERTY IMPORTED_LOCATION_DEBUG
${CMAKE_CURRENT_BINARY_DIR}/debug/byondrs.dll)
set_property(
TARGET byondrs PROPERTY IMPORTED_IMPLIB_DEBUG
${CMAKE_CURRENT_BINARY_DIR}/debug/byondrs.dll.lib)
elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set_property(
TARGET byondrs
PROPERTY IMPORTED_LOCATION
${CMAKE_CURRENT_BINARY_DIR}/release/libbyondrs.dylib)
set_property(
TARGET byondrs PROPERTY IMPORTED_LOCATION_DEBUG
${CMAKE_CURRENT_BINARY_DIR}/debug/libbyondrs.dylib)
else ()
set_property(
TARGET byondrs PROPERTY IMPORTED_LOCATION
${CMAKE_CURRENT_BINARY_DIR}/release/libbyondrs.so)
set_property(
TARGET byondrs PROPERTY IMPORTED_LOCATION_DEBUG
${CMAKE_CURRENT_BINARY_DIR}/debug/libbyondrs.so)
endif ()

if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
add_test(
NAME byondrs_test
COMMAND cargo test
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
endif ()
5 changes: 0 additions & 5 deletions c-ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

_Unofficial C library for working with BYOND data._

## TODO

This library doesn't currently export any C projects (e.g. CMake) for
easy inclusion in external projects.

## License

Licensed under either of
Expand Down

0 comments on commit 8837bd8

Please sign in to comment.