target_compile_options_if_exists.cmake 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # This file is part of Desktop App Toolkit,
  2. # a set of libraries for developing nice desktop applications.
  3. #
  4. # For license and copyright information please follow this link:
  5. # https://github.com/desktop-app/legal/blob/master/LEGAL
  6. include(CheckCXXCompilerFlag)
  7. function(target_compile_options_if_exists target_name)
  8. set(writing_now "")
  9. set(private_options "")
  10. set(public_options "")
  11. set(interface_options "")
  12. foreach (entry ${ARGN})
  13. if (${entry} STREQUAL "PRIVATE" OR ${entry} STREQUAL "PUBLIC" OR ${entry} STREQUAL "INTERFACE")
  14. set(writing_now ${entry})
  15. else()
  16. string(MAKE_C_IDENTIFIER ${entry} entry_identifier)
  17. check_cxx_compiler_flag(${entry} DESKTOP_APP_${entry_identifier}_EXISTS)
  18. if (DESKTOP_APP_${entry_identifier}_EXISTS)
  19. if ("${writing_now}" STREQUAL "PRIVATE")
  20. list(APPEND private_options ${entry})
  21. elseif ("${writing_now}" STREQUAL "PUBLIC")
  22. list(APPEND public_options ${entry})
  23. elseif ("${writing_now}" STREQUAL "INTERFACE")
  24. list(APPEND interface_options ${entry})
  25. else()
  26. message(FATAL_ERROR "Unknown options scope for target ${target_name}")
  27. endif()
  28. endif()
  29. endif()
  30. endforeach()
  31. if (NOT "${public_options}" STREQUAL "")
  32. target_compile_options(${target_name} PUBLIC ${public_options})
  33. endif()
  34. if (NOT "${private_options}" STREQUAL "")
  35. target_compile_options(${target_name} PRIVATE ${private_options})
  36. endif()
  37. if (NOT "${interface_options}" STREQUAL "")
  38. target_compile_options(${target_name} INTERFACE ${interface_options})
  39. endif()
  40. endfunction()