Skip to content

Commit

Permalink
Modernize CMakeLists
Browse files Browse the repository at this point in the history
  • Loading branch information
Maratyszcza committed May 27, 2024
1 parent 80bd322 commit 0a01ab2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 36 deletions.
95 changes: 61 additions & 34 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12 FATAL_ERROR)
CMAKE_MINIMUM_REQUIRED(VERSION 3.16 FATAL_ERROR)

# ---[ Project
PROJECT(FP16 C CXX)
Expand All @@ -12,19 +12,6 @@ IF(FP16_BUILD_TESTS)
ENABLE_TESTING()
ENDIF()

MACRO(FP16_TARGET_ENABLE_CXX11 target)
IF(${CMAKE_VERSION} VERSION_LESS "3.1")
IF(NOT MSVC)
TARGET_COMPILE_OPTIONS(${target} PRIVATE -std=c++11)
ENDIF()
ELSE()
SET_TARGET_PROPERTIES(${target} PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS YES)
ENDIF()
ENDMACRO()

# ---[ Download deps
IF(FP16_BUILD_TESTS AND NOT DEFINED GOOGLETEST_SOURCE_DIR)
MESSAGE(STATUS "Downloading Google Test to ${CMAKE_BINARY_DIR}/googletest-source (define GOOGLETEST_SOURCE_DIR to avoid it)")
Expand All @@ -49,25 +36,17 @@ ENDIF()
INCLUDE(GNUInstallDirs)

# ---[ FP16 library
IF(${CMAKE_VERSION} VERSION_LESS "3.0")
ADD_LIBRARY(fp16 STATIC
include/fp16.h
include/fp16/fp16.h
include/fp16/bitcasts.h)
SET_TARGET_PROPERTIES(fp16 PROPERTIES LINKER_LANGUAGE C)
ELSE()
ADD_LIBRARY(fp16 INTERFACE)
ENDIF()
ADD_LIBRARY(fp16 INTERFACE)
TARGET_INCLUDE_DIRECTORIES(fp16 INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:include>")

INSTALL(FILES include/fp16.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
INSTALL(FILES
include/fp16/bitcasts.h
include/fp16/fp16.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/fp16)
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/fp16")

IF(FP16_BUILD_TESTS)
# ---[ Build google test
Expand All @@ -80,36 +59,64 @@ IF(FP16_BUILD_TESTS)

# ---[ Build FP16 unit tests
ADD_EXECUTABLE(ieee-to-fp32-bits-test test/ieee-to-fp32-bits.cc test/tables.cc)
SET_TARGET_PROPERTIES(ieee-to-fp32-bits-test PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS YES)
TARGET_INCLUDE_DIRECTORIES(ieee-to-fp32-bits-test PRIVATE test)
TARGET_LINK_LIBRARIES(ieee-to-fp32-bits-test fp16 gtest gtest_main)
ADD_TEST(ieee-to-fp32-bits ieee-to-fp32-bits-test)

ADD_EXECUTABLE(ieee-to-fp32-value-test test/ieee-to-fp32-value.cc test/tables.cc)
SET_TARGET_PROPERTIES(ieee-to-fp32-value-test PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS YES)
TARGET_INCLUDE_DIRECTORIES(ieee-to-fp32-value-test PRIVATE test)
TARGET_LINK_LIBRARIES(ieee-to-fp32-value-test fp16 gtest gtest_main)
ADD_TEST(ieee-to-fp32-value ieee-to-fp32-value-test)

ADD_EXECUTABLE(ieee-from-fp32-value-test test/ieee-from-fp32-value.cc test/tables.cc)
SET_TARGET_PROPERTIES(ieee-from-fp32-value-test PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS YES)
TARGET_INCLUDE_DIRECTORIES(ieee-from-fp32-value-test PRIVATE test)
TARGET_LINK_LIBRARIES(ieee-from-fp32-value-test fp16 gtest gtest_main)
ADD_TEST(ieee-from-fp32-value ieee-from-fp32-value-test)

ADD_EXECUTABLE(alt-to-fp32-bits-test test/alt-to-fp32-bits.cc test/tables.cc)
SET_TARGET_PROPERTIES(alt-to-fp32-bits-test PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS YES)
TARGET_INCLUDE_DIRECTORIES(alt-to-fp32-bits-test PRIVATE test)
TARGET_LINK_LIBRARIES(alt-to-fp32-bits-test fp16 gtest gtest_main)
ADD_TEST(alt-to-fp32-bits alt-to-fp32-bits-test)

ADD_EXECUTABLE(alt-to-fp32-value-test test/alt-to-fp32-value.cc test/tables.cc)
SET_TARGET_PROPERTIES(alt-to-fp32-value-test PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS YES)
TARGET_INCLUDE_DIRECTORIES(alt-to-fp32-value-test PRIVATE test)
TARGET_LINK_LIBRARIES(alt-to-fp32-value-test fp16 gtest gtest_main)
ADD_TEST(alt-to-fp32-value alt-to-fp32-value-test)

ADD_EXECUTABLE(alt-from-fp32-value-test test/alt-from-fp32-value.cc test/tables.cc)
SET_TARGET_PROPERTIES(alt-from-fp32-value-test PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS YES)
TARGET_INCLUDE_DIRECTORIES(alt-from-fp32-value-test PRIVATE test)
TARGET_LINK_LIBRARIES(alt-from-fp32-value-test fp16 gtest gtest_main)
ADD_TEST(alt-from-fp32-value alt-from-fp32-value-test)

ADD_EXECUTABLE(bitcasts-test test/bitcasts.cc)
SET_TARGET_PROPERTIES(bitcasts-test PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS YES)
TARGET_LINK_LIBRARIES(bitcasts-test fp16 gtest gtest_main)
ADD_TEST(bitcasts bitcasts-test)
ENDIF()
Expand All @@ -125,30 +132,50 @@ IF(FP16_BUILD_BENCHMARKS)

# ---[ Build FP16 benchmarks
ADD_EXECUTABLE(ieee-element-bench bench/ieee-element.cc)
SET_TARGET_PROPERTIES(ieee-element-bench PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS YES)
TARGET_COMPILE_DEFINITIONS(ieee-element-bench PRIVATE FP16_COMPARATIVE_BENCHMARKS=1)
TARGET_INCLUDE_DIRECTORIES(ieee-element-bench PRIVATE ${PROJECT_SOURCE_DIR})
TARGET_INCLUDE_DIRECTORIES(ieee-element-bench PRIVATE "${PROJECT_SOURCE_DIR}")
TARGET_LINK_LIBRARIES(ieee-element-bench fp16 benchmark)

ADD_EXECUTABLE(alt-element-bench bench/alt-element.cc)
SET_TARGET_PROPERTIES(alt-element-bench PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS YES)
TARGET_LINK_LIBRARIES(alt-element-bench fp16 benchmark)

ADD_EXECUTABLE(from-ieee-array-bench bench/from-ieee-array.cc)
FP16_TARGET_ENABLE_CXX11(from-ieee-array-bench)
SET_TARGET_PROPERTIES(from-ieee-array-bench PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS YES)
TARGET_COMPILE_DEFINITIONS(from-ieee-array-bench PRIVATE FP16_COMPARATIVE_BENCHMARKS=1)
TARGET_INCLUDE_DIRECTORIES(from-ieee-array-bench PRIVATE ${PROJECT_SOURCE_DIR})
TARGET_INCLUDE_DIRECTORIES(from-ieee-array-bench PRIVATE "${PROJECT_SOURCE_DIR}")
TARGET_LINK_LIBRARIES(from-ieee-array-bench fp16 benchmark)

ADD_EXECUTABLE(from-alt-array-bench bench/from-alt-array.cc)
FP16_TARGET_ENABLE_CXX11(from-alt-array-bench)
SET_TARGET_PROPERTIES(from-alt-array-bench PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS YES)
TARGET_LINK_LIBRARIES(from-alt-array-bench fp16 benchmark)

ADD_EXECUTABLE(to-ieee-array-bench bench/to-ieee-array.cc)
FP16_TARGET_ENABLE_CXX11(to-ieee-array-bench)
SET_TARGET_PROPERTIES(to-ieee-array-bench PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS YES)
TARGET_COMPILE_DEFINITIONS(to-ieee-array-bench PRIVATE FP16_COMPARATIVE_BENCHMARKS=1)
TARGET_INCLUDE_DIRECTORIES(to-ieee-array-bench PRIVATE ${PROJECT_SOURCE_DIR})
TARGET_INCLUDE_DIRECTORIES(to-ieee-array-bench PRIVATE "${PROJECT_SOURCE_DIR}")
TARGET_LINK_LIBRARIES(to-ieee-array-bench fp16 benchmark)

ADD_EXECUTABLE(to-alt-array-bench bench/to-alt-array.cc)
FP16_TARGET_ENABLE_CXX11(to-alt-array-bench)
SET_TARGET_PROPERTIES(to-alt-array-bench PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS YES)
TARGET_LINK_LIBRARIES(to-alt-array-bench fp16 benchmark)
ENDIF()
2 changes: 1 addition & 1 deletion cmake/DownloadGoogleBenchmark.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12 FATAL_ERROR)
CMAKE_MINIMUM_REQUIRED(VERSION 3.16 FATAL_ERROR)

PROJECT(googlebenchmark-download NONE)

Expand Down
2 changes: 1 addition & 1 deletion cmake/DownloadGoogleTest.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12 FATAL_ERROR)
CMAKE_MINIMUM_REQUIRED(VERSION 3.16 FATAL_ERROR)

PROJECT(googletest-download NONE)

Expand Down

0 comments on commit 0a01ab2

Please sign in to comment.