igl_add_library.cmake 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Creates a CMake target for a given libigl module. This convenience function also defines common
  2. # compilation flags, as well as installation rules for the target. Target sources and dependencies
  3. # need to be added separately.
  4. function(igl_add_library module_name)
  5. # Check if category is `copyleft` or `restricted`
  6. if(${module_name} MATCHES "^igl_copyleft")
  7. set(suffix "_copyleft")
  8. elseif(${module_name} MATCHES "^igl_restricted")
  9. set(suffix "_restricted")
  10. else()
  11. set(suffix "")
  12. endif()
  13. # Check module name
  14. if(NOT ${module_name} MATCHES "^igl_")
  15. message(FATAL_ERROR "Libigl module name should start with 'igl_'")
  16. endif()
  17. string(REPLACE "igl${suffix}_" "" module_shortname ${module_name})
  18. # Define target
  19. if(LIBIGL_USE_STATIC_LIBRARY)
  20. add_library(${module_name} STATIC)
  21. else()
  22. add_library(${module_name} INTERFACE)
  23. endif()
  24. # Alias target name
  25. message(STATUS "Creating target: igl${suffix}::${module_shortname} (${module_name})")
  26. add_library(igl${suffix}::${module_shortname} ALIAS ${module_name})
  27. # Compile definitions
  28. if(LIBIGL_USE_STATIC_LIBRARY)
  29. target_compile_definitions(${module_name} ${IGL_SCOPE} -DIGL_STATIC_LIBRARY)
  30. endif()
  31. # C++11 features
  32. target_compile_features(${module_name} ${IGL_SCOPE} cxx_std_11)
  33. # Other compilation flags
  34. if(MSVC)
  35. # Enable parallel compilation for Visual Studio
  36. target_compile_options(${module_name} ${IGL_SCOPE} $<$<COMPILE_LANGUAGE:CXX>:/MP> $<$<COMPILE_LANGUAGE:CXX>:/bigobj>)
  37. target_compile_definitions(${module_name} ${IGL_SCOPE} -DNOMINMAX)
  38. # Silencing some compilation warnings
  39. if(LIBIGL_USE_STATIC_LIBRARY)
  40. target_compile_options(${module_name} PRIVATE
  41. # Type conversion warnings. These can be fixed with some effort and possibly more verbose code.
  42. /wd4267 # conversion from 'size_t' to 'type', possible loss of data
  43. /wd4244 # conversion from 'type1' to 'type2', possible loss of data
  44. /wd4018 # signed/unsigned mismatch
  45. /wd4305 # truncation from 'double' to 'float'
  46. # This one is from template instantiations generated by autoexplicit.sh:
  47. /wd4667 # no function template defined that matches forced instantiation ()
  48. # This one is easy to fix, just need to switch to safe version of C functions
  49. /wd4996 # this function or variable may be unsafe
  50. # This one is when using bools in adjacency matrices
  51. /wd4804 #'+=': unsafe use of type 'bool' in operation
  52. )
  53. endif()
  54. endif()
  55. # Generate position independent code
  56. if(LIBIGL_POSITION_INDEPENDENT_CODE)
  57. set_target_properties(${module_name} PROPERTIES INTERFACE_POSITION_INDEPENDENT_CODE ON)
  58. if(LIBIGL_USE_STATIC_LIBRARY)
  59. set_target_properties(${module_name} PROPERTIES POSITION_INDEPENDENT_CODE ON)
  60. endif()
  61. endif()
  62. # Folder for IDE
  63. if(LIBIGL_USE_STATIC_LIBRARY OR CMAKE_VERSION VERSION_GREATER_EQUAL 3.19.0)
  64. set_target_properties(${module_name} PROPERTIES FOLDER "Libigl")
  65. endif()
  66. endfunction()