You can access the default compile flags that cmake uses per configuration (see this answer). You can also set the COMPILE_FLAGS property on any source file. Thus, after you have created a target, it should be as simple as querying the default compile options for your language, and applying the appropriate defaults using set_source_files_properties
.
For example, here's a CMakeLists.txt for an extremely simple project:
cmake_minimum_required(VERSION 3.6.1)
project(MyProj)
set_source_files_properties(file1.cpp PROPERTIES COMPILE_FLAGS ${CMAKE_CXX_FLAGS_RELEASE})
get_source_file_property(FILE1_FLAGS file1.cpp COMPILE_FLAGS)
get_source_file_property(FILE2_FLAGS file2.cpp COMPILE_FLAGS)
message( "\${FILE1_FLAGS} = ${FILE1_FLAGS}")
message( "\${FILE2_FLAGS} = ${FILE2_FLAGS}")
add_executable( MyExe file1.cpp file2.cpp )
Which outputs (for MSVC):
${FILE1_FLAGS} = /MD /O2 /Ob2 /DNDEBUG
${FILE2_FLAGS} = NOTFOUND
If you inspect the generated MyExe.vcxproj, you'll see that defaults are used to compile file2.cpp, whereas for file1.cpp, all the configurations use release settings:
<ClCompile Include="C:\temp\cmake\file1.cpp">
<InlineFunctionExpansion Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">AnySuitable</InlineFunctionExpansion>
<Optimization Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">MaxSpeed</Optimization>
<RuntimeLibrary Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">MultiThreadedDLL</RuntimeLibrary>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<InlineFunctionExpansion Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">AnySuitable</InlineFunctionExpansion>
<Optimization Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">MaxSpeed</Optimization>
<RuntimeLibrary Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">MultiThreadedDLL</RuntimeLibrary>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<InlineFunctionExpansion Condition="'$(Configuration)|$(Platform)'=='MinSizeRel|Win32'">AnySuitable</InlineFunctionExpansion>
<Optimization Condition="'$(Configuration)|$(Platform)'=='MinSizeRel|Win32'">MaxSpeed</Optimization>
<RuntimeLibrary Condition="'$(Configuration)|$(Platform)'=='MinSizeRel|Win32'">MultiThreadedDLL</RuntimeLibrary>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='MinSizeRel|Win32'">NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<InlineFunctionExpansion Condition="'$(Configuration)|$(Platform)'=='RelWithDebInfo|Win32'">AnySuitable</InlineFunctionExpansion>
<Optimization Condition="'$(Configuration)|$(Platform)'=='RelWithDebInfo|Win32'">MaxSpeed</Optimization>
<RuntimeLibrary Condition="'$(Configuration)|$(Platform)'=='RelWithDebInfo|Win32'">MultiThreadedDLL</RuntimeLibrary>
<PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='RelWithDebInfo|Win32'">NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<ClCompile Include="C:\temp\cmake\file2.cpp" />
I haven't tried this on other generators, but it should work identically.