1# Copyright 2022 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15cmake_minimum_required(VERSION 3.14)
16
17project(Ukey2)
18
19enable_testing()
20
21include_directories(
22    ${CMAKE_SOURCE_DIR}/ukey2_c_ffi/cpp/)
23
24include(GoogleTest)
25include(ExternalProject)
26
27set_directory_properties(PROPERTIES EP_PREFIX ${CMAKE_BINARY_DIR}/target/tmp)
28ExternalProject_Add(
29    ukey2_c_ffi
30    DOWNLOAD_COMMAND ""
31    CONFIGURE_COMMAND ""
32    BUILD_COMMAND cargo build COMMAND cargo build --release --lib
33    BINARY_DIR "${CMAKE_SOURCE_DIR}/ukey2_c_ffi"
34    INSTALL_COMMAND "")
35
36# required for designated initializers on MSVC
37set(CMAKE_CXX_STANDARD 20)
38
39if(UNIX)
40    add_compile_options(-Wall -Wextra -Wimplicit-fallthrough -Wextra-semi
41            -Wno-missing-field-initializers -Wno-unused-parameter -Wno-psabi
42            -Wshadow
43            -Wsign-compare)
44elseif(MSVC)
45    add_compile_options(-W4 -MD)
46endif()
47
48include(FetchContent)
49FetchContent_Declare(
50  googletest
51  GIT_REPOSITORY https://github.com/google/googletest.git
52  GIT_TAG release-1.12.1
53)
54FetchContent_MakeAvailable(googletest)
55
56add_executable(ffi_test
57  ukey2_test.cc
58  ukey2_glue.cc
59  ukey2_ffi.h
60  ukey2_bindings.h)
61
62set(ukey2_c_ffi_FILENAME "${CMAKE_SHARED_LIBRARY_PREFIX}ukey2_c_ffi${CMAKE_SHARED_LIBRARY_SUFFIX}")
63
64if(UNIX)
65    target_link_libraries(
66        ffi_test
67        "${CMAKE_SOURCE_DIR}/../../../../target/release/${ukey2_c_ffi_FILENAME}"
68        GTest::gtest_main
69        dl pthread
70    )
71elseif(MSVC)
72    # MSVC requires linking to a static lib, which rust kindly generates
73    target_link_libraries(
74        ffi_test
75        "${CMAKE_SOURCE_DIR}/../../../../target/release/${ukey2_c_ffi_FILENAME}${CMAKE_STATIC_LIBRARY_SUFFIX}"
76        GTest::gtest_main
77    )
78    # MSVC requires that the dynamic lib be visible to the binary, and copying to the binary dir is an easy way to do that
79    get_target_property(ffi_test_BINARY_DIR ffi_test BINARY_DIR)
80    add_custom_command(
81        TARGET ffi_test POST_BUILD
82        COMMAND "${CMAKE_COMMAND}" -E copy "${CMAKE_SOURCE_DIR}/../../../../target/release/${ukey2_c_ffi_FILENAME}" "${ffi_test_BINARY_DIR}/."
83    )
84endif()
85
86gtest_discover_tests(ffi_test)
87