# Fix linking with OpenMP, fix installation.

--- CMakeLists.txt.orig	2020-05-19 18:05:33.000000000 +0800
+++ CMakeLists.txt	2022-11-01 12:02:01.000000000 +0800
@@ -14,36 +14,44 @@
 
 if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
 
-  # assumes clang build
-  # we can't reliably detect when we're using clang, so for the time being we assume
-  # TODO: can't we though?
-  
   # adapted from https://stackoverflow.com/questions/46414660/macos-cmake-and-openmp
   # find_package(OpenMP) does not work reliably on macOS, so we do its work ourselves
   set (OpenMP_C "${CMAKE_C_COMPILER}")
-  set (OpenMP_C_FLAGS " -Xpreprocessor -fopenmp -I/opt/local/include/libomp -I/usr/local/include -L/opt/local/lib/libomp -L/usr/local/lib")
-  set (OpenMP_C_LIB_NAMES "libomp" "libgomp" "libiomp5")
   set (OpenMP_CXX "${CMAKE_CXX_COMPILER}")
-  set (OpenMP_CXX_FLAGS " -Xpreprocessor -fopenmp -I/opt/local/include/libomp -I/usr/local/include -L/opt/local/lib/libomp -L/usr/local/lib")
-  set (OpenMP_CXX_LIB_NAMES "libomp" "libgomp" "libiomp5")
-  set (OpenMP_libomp_LIBRARY "omp")
-  set (OpenMP_libgomp_LIBRARY "gomp")
-  set (OpenMP_libiomp5_LIBRARY "iomp5")
-  
+
+  if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+    set (OpenMP_C_FLAGS " -Xpreprocessor -fopenmp -I/@PREFIX@/include/libomp -L/@PREFIX@/lib/libomp")
+    set (OpenMP_CXX_FLAGS " -Xpreprocessor -fopenmp -I/@PREFIX@/include/libomp -L/@PREFIX@/lib/libomp")
+    set (OpenMP_C_LIB_NAMES "libomp")
+    set (OpenMP_CXX_LIB_NAMES "libomp")
+    set (OpenMP_libomp_LIBRARY "omp")
+  else()
+    set (OpenMP_C_FLAGS " -Xpreprocessor -fopenmp")
+    set (OpenMP_CXX_FLAGS " -Xpreprocessor -fopenmp")
+    set (OpenMP_C_LIB_NAMES "libgomp")
+    set (OpenMP_CXX_LIB_NAMES  "libgomp")
+    set (OpenMP_libgomp_LIBRARY "gomp")
+  endif()
+
   # and now add the OpenMP parameters to the compile flags
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
   set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
-  set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS} -lomp")
-  
+  if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
+    set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS} -lomp")
+  else()
+  # When using GCC, -fopenmp ensures that libgomp is linked with.
+    set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
+  endif()
+
 elseif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
 
   find_package(OpenMP REQUIRED)
-  
+
   # add the flags it detects to the compile flags
   set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS} -fopenmp")
   set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS} -fopenmp")
   set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
-  
+
 endif()
 
 # Set the output folder where your program will be created
@@ -67,9 +75,17 @@
 set(atomicbitvector_INCLUDES
   "${CMAKE_SOURCE_DIR}/include")
 
-add_executable(test_atomicbitvector
-  ${CMAKE_SOURCE_DIR}/src/main.cpp)
-target_include_directories(test_atomicbitvector PUBLIC ${atomicbitvector_INCLUDES})
+option (build_tests "Build atomicbitvector tests" ON)
+
+if (build_tests)
+  add_subdirectory(src)
+  add_executable(test_atomicbitvector
+    ${CMAKE_SOURCE_DIR}/src/main.cpp)
+  target_include_directories(test_atomicbitvector PUBLIC ${atomicbitvector_INCLUDES})
+  add_executable(test_atomicbitvector_extra
+    ${CMAKE_SOURCE_DIR}/src/main1.cpp)
+  target_include_directories(test_atomicbitvector_extra PUBLIC ${atomicbitvector_INCLUDES})
+endif (build_tests)
 
 if (APPLE)
 elseif (TRUE)
@@ -77,3 +93,26 @@
     set(CMAKE_EXE_LINKER_FLAGS "-static")
   endif()
 endif()
+
+install(FILES ${CMAKE_SOURCE_DIR}/include/atomic_bitvector.hpp ${CMAKE_SOURCE_DIR}/include/iterator_tpl.h
+        DESTINATION ${CMAKE_INSTALL_PREFIX}/include/atomicbitvector)
+
+add_library(atomicbitvector INTERFACE)
+target_include_directories(
+    atomicbitvector
+    INTERFACE
+    "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>"
+    "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
+)
+
+install(
+    TARGETS atomicbitvector
+    EXPORT atomicbitvectorExport
+    INCLUDES DESTINATION "${CMAKE_INSTALL_PREFIX}/include/atomicbitvector"
+)
+
+install(
+    EXPORT atomicbitvectorExport
+    DESTINATION "${CMAKE_INSTALL_PREFIX}/lib/cmake/atomicbitvector"
+    FILE "atomicbitvectorConfig.cmake"
+)
