utils.cmake 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright 2017 Google
  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. include(CMakeParseArguments)
  15. # cc_library(
  16. # target
  17. # SOURCES sources...
  18. # DEPENDS libraries...
  19. # )
  20. #
  21. # Defines a new library target with the given target name, sources, and dependencies.
  22. function(cc_library name)
  23. set(flag EXCLUDE_FROM_ALL)
  24. set(multi DEPENDS SOURCES)
  25. cmake_parse_arguments(ccl "${flag}" "" "${multi}" ${ARGN})
  26. add_library(
  27. ${name}
  28. ${ccl_SOURCES}
  29. )
  30. add_objc_flags(${name} ccl)
  31. target_link_libraries(
  32. ${name}
  33. PUBLIC
  34. ${ccl_DEPENDS}
  35. )
  36. if(ccl_EXCLUDE_FROM_ALL)
  37. set_property(
  38. TARGET ${name}
  39. PROPERTY EXCLUDE_FROM_ALL ON
  40. )
  41. endif()
  42. endfunction()
  43. # cc_test(
  44. # target
  45. # SOURCES sources...
  46. # DEPENDS libraries...
  47. # )
  48. #
  49. # Defines a new test executable target with the given target name, sources, and
  50. # dependencies. Implicitly adds DEPENDS on GTest::GTest and GTest::Main.
  51. function(cc_test name)
  52. set(multi DEPENDS SOURCES)
  53. cmake_parse_arguments(cct "" "" "${multi}" ${ARGN})
  54. list(APPEND cct_DEPENDS GTest::GTest GTest::Main)
  55. add_executable(${name} ${cct_SOURCES})
  56. add_objc_flags(${name} cct)
  57. add_test(${name} ${name})
  58. target_link_libraries(${name} ${cct_DEPENDS})
  59. endfunction()
  60. # add_objc_flags(target sources...)
  61. #
  62. # Adds OBJC_FLAGS to the compile options of the given target if any of the
  63. # sources have filenames that indicate they are are Objective-C.
  64. function(add_objc_flags target)
  65. set(_has_objc OFF)
  66. foreach(source ${ARGN})
  67. get_filename_component(ext ${source} EXT)
  68. if((ext STREQUAL ".m") OR (ext STREQUAL ".mm"))
  69. set(_has_objc ON)
  70. endif()
  71. endforeach()
  72. if(_has_objc)
  73. target_compile_options(
  74. ${target}
  75. PRIVATE
  76. ${OBJC_FLAGS}
  77. )
  78. endif()
  79. endfunction()