Git conversion: Make reactos the root directory, move rosapps, rostests, wallpapers...
[reactos.git] / sdk / cmake / compilerflags.cmake
1
2 # add_target_property
3 # Adds one or more values to the specified property of the specified target.
4 # Note that there are properties which require (semicolon-separated) lists,
5 # while others require space-separated strings. The function has a list of
6 # properties of the former variety and handles the values accordingly
7 function(add_target_property _module _propname)
8 list(APPEND _list_properties COMPILE_DEFINITIONS INCLUDE_DIRECTORIES)
9 set(_newvalue "")
10 get_target_property(_oldvalue ${_module} ${_propname})
11 if(_oldvalue)
12 set(_newvalue ${_oldvalue})
13 endif()
14 list(FIND _list_properties ${_propname} _list_index)
15 if(NOT _list_index EQUAL -1)
16 # list property
17 list(APPEND _newvalue ${ARGN})
18 else()
19 # string property
20 foreach(_flag ${ARGN})
21 set(_newvalue "${_newvalue} ${_flag}")
22 endforeach()
23 endif()
24 set_property(TARGET ${_module} PROPERTY ${_propname} ${_newvalue})
25 endfunction()
26
27 # Wrapper functions for the important properties, using add_target_property
28 # where appropriate.
29 # Note that the functions for string properties take a single string
30 # argument while those for list properties can take a variable number of
31 # arguments, all of which will be added to the list
32 #
33 # Examples:
34 # add_target_compile_flags(mymodule "-pedantic -O5")
35 # add_target_link_flags(mymodule "-s --fatal-warnings")
36 # add_target_compile_definitions(mymodule WIN32 _WIN32 INLINE=inline)
37 # add_target_include_directories(mymodule include ../include)
38 function(add_target_compile_flags _module _flags)
39 if(${ARGC} GREATER 2)
40 message(FATAL_ERROR "Excess arguments to add_target_compile_flags! Module ${_module}, args ${ARGN}")
41 endif()
42 add_target_property(${_module} COMPILE_FLAGS ${_flags})
43 endfunction()
44
45 function(add_target_link_flags _module _flags)
46 if(${ARGC} GREATER 2)
47 message(FATAL_ERROR "Excess arguments to add_target_link_flags! Module ${_module}, args ${ARGN}")
48 endif()
49 add_target_property(${_module} LINK_FLAGS ${_flags})
50 endfunction()
51
52 function(add_target_compile_definitions _module)
53 add_target_property(${_module} COMPILE_DEFINITIONS ${ARGN})
54 endfunction()
55
56 function(add_target_include_directories _module)
57 add_target_property(${_module} INCLUDE_DIRECTORIES ${ARGN})
58 endfunction()
59
60 # replace_compiler_option
61 # (taken from LLVM)
62 # Replaces a compiler option or switch `_old' in `_var' by `_new'.
63 # If `_old' is not in `_var', appends `_new' to `_var'.
64 #
65 # Example:
66 # replace_compiler_option(CMAKE_CXX_FLAGS_RELEASE "-O3" "-O2")
67 macro(replace_compiler_option _var _old _new)
68 # If the option already is on the variable, don't add it:
69 if("${${_var}}" MATCHES "(^| )${_new}($| )")
70 set(__n "")
71 else()
72 set(__n "${_new}")
73 endif()
74 if("${${_var}}" MATCHES "(^| )${_old}($| )")
75 string(REGEX REPLACE "(^| )${_old}($| )" " ${__n} " ${_var} "${${_var}}")
76 else()
77 set(${_var} "${${_var}} ${__n}")
78 endif()
79 endmacro(replace_compiler_option)
80
81 # add_compile_flags
82 # add_compile_flags_language
83 # replace_compile_flags
84 # replace_compile_flags_language
85 # Add or replace compiler flags in the global scope for either all source
86 # files or only those of the specified language.
87 #
88 # Examples:
89 # add_compile_flags("-pedantic -O5")
90 # add_compile_flags_language("-std=gnu99" "C")
91 # replace_compile_flags("-O5" "-O3")
92 # replace_compile_flags_language("-fno-exceptions" "-fexceptions" "CXX")
93 function(add_compile_flags _flags)
94 if(${ARGC} GREATER 1)
95 message(FATAL_ERROR "Excess arguments to add_compile_flags! Args ${ARGN}")
96 endif()
97 # Adds the compiler flag for all code files: C, C++, and assembly
98 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${_flags}" PARENT_SCOPE)
99 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${_flags}" PARENT_SCOPE)
100 set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${_flags}" PARENT_SCOPE)
101 endfunction()
102
103 function(add_compile_flags_language _flags _lang)
104 if(NOT ${ARGC} EQUAL 2)
105 message(FATAL_ERROR "Wrong arguments to add_compile_flags_language! Args ${ARGN}")
106 endif()
107 # Adds the compiler flag for the specified language only, e.g. CMAKE_C_FLAGS
108 set(CMAKE_${_lang}_FLAGS "${CMAKE_${_lang}_FLAGS} ${_flags}" PARENT_SCOPE)
109 endfunction()
110
111 macro(replace_compile_flags _oldflags _newflags)
112 if(NOT ${ARGC} EQUAL 2)
113 message(FATAL_ERROR "Wrong arguments to replace_compile_flags! Args ${ARGN}")
114 endif()
115 replace_compiler_option(CMAKE_C_FLAGS ${_oldflags} ${_newflags})
116 replace_compiler_option(CMAKE_CXX_FLAGS ${_oldflags} ${_newflags})
117 replace_compiler_option(CMAKE_ASM_FLAGS ${_oldflags} ${_newflags})
118 endmacro()
119
120 macro(replace_compile_flags_language _oldflags _newflags _lang)
121 if(NOT ${ARGC} EQUAL 3)
122 message(FATAL_ERROR "Wrong arguments to replace_compile_flags_language! Args ${ARGN}")
123 endif()
124 replace_compiler_option(CMAKE_${_lang}_FLAGS ${_oldflags} ${_newflags})
125 endmacro()