Skip to content

Commit f8a1a42

Browse files
committed
Unify duplicated cmake code between CPU and GPU builds
1 parent f528b0d commit f8a1a42

5 files changed

Lines changed: 119 additions & 194 deletions

File tree

cmake/modules/CppLibrary.cmake

Lines changed: 78 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,72 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7+
include_guard(GLOBAL)
8+
9+
# Shared compiler warning flags for both CPU and GPU builds
10+
function(fbgemm_get_warning_flags)
11+
cmake_parse_arguments(ARG "" "MSVC_FLAGS_VAR;CC_FLAGS_VAR"
12+
"EXTRA_MSVC_FLAGS;EXTRA_CC_FLAGS" ${ARGN})
13+
14+
# MSVC flags
15+
set(_msvc
16+
${ARG_EXTRA_MSVC_FLAGS}
17+
/wd4244
18+
/wd4267
19+
/wd4305
20+
/wd4309)
21+
22+
# Common GCC/Clang flags
23+
set(_cc
24+
${ARG_EXTRA_CC_FLAGS}
25+
-Wall
26+
-Wextra
27+
-Werror
28+
-Wunknown-pragmas
29+
-Wimplicit-fallthrough
30+
-Wno-deprecated-declarations
31+
-Wno-deprecated-enum-enum-conversion
32+
-Wno-strict-aliasing
33+
-Wunused-variable
34+
-Wno-sign-compare
35+
-Wno-vla
36+
-Wno-error=unused-parameter
37+
-Wno-error=unknown-pragmas
38+
-Wno-error=attributes)
39+
40+
# Clang-specific
41+
if(CMAKE_CXX_COMPILER_ID MATCHES Clang)
42+
list(APPEND _cc
43+
-Wno-unused-command-line-argument
44+
-Wno-c99-extensions
45+
-Wno-gnu-zero-variadic-macro-arguments)
46+
47+
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 13.0.0)
48+
list(APPEND _cc
49+
-Wno-error=unused-but-set-parameter
50+
-Wno-error=unused-but-set-variable)
51+
endif()
52+
53+
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 17.0.0)
54+
list(APPEND _cc
55+
-Wno-vla-cxx-extension
56+
-Wno-error=global-constructors
57+
-Wno-error=shadow)
58+
endif()
59+
60+
# GNU-specific
61+
elseif(CMAKE_CXX_COMPILER_ID STREQUAL GNU)
62+
list(APPEND _cc
63+
-Wmaybe-uninitialized
64+
-Wno-error=unused-but-set-parameter
65+
-Wno-error=unused-but-set-variable
66+
-Wno-error=array-bounds)
67+
endif()
68+
69+
set(${ARG_MSVC_FLAGS_VAR} ${_msvc} PARENT_SCOPE)
70+
set(${ARG_CC_FLAGS_VAR} ${_cc} PARENT_SCOPE)
71+
endfunction()
72+
773
function(cpp_library)
874
# NOTE: This function is meant for building targets in FBGEMM, not
975
# FBGEMM_GPU or FBGEMM GenAI, which have much more complicated setups.
@@ -83,55 +149,20 @@ function(cpp_library)
83149
PUBLIC FBGEMM_STATIC)
84150
endif()
85151

86-
set(lib_cc_flags
87-
${args_MSVC_FLAGS}
88-
/wd4244
89-
/wd4267
90-
/wd4305
91-
/wd4309)
152+
fbgemm_get_warning_flags(
153+
MSVC_FLAGS_VAR _msvc_flags
154+
CC_FLAGS_VAR _cc_flags
155+
EXTRA_MSVC_FLAGS ${args_MSVC_FLAGS}
156+
EXTRA_CC_FLAGS ${args_CC_FLAGS})
157+
set(lib_cc_flags ${_msvc_flags})
92158

93159
else()
94-
set(lib_cc_flags
95-
${args_CC_FLAGS}
96-
-Wno-deprecated-declarations
97-
-Wall
98-
-Wextra
99-
-Werror
100-
-Wunknown-pragmas
101-
-Wimplicit-fallthrough
102-
-Wno-strict-aliasing
103-
-Wunused-variable
104-
-Wno-sign-compare
105-
-Wno-vla
106-
-Wno-error=unused-parameter
107-
-Wno-error=attributes)
108-
109-
if(CMAKE_CXX_COMPILER_ID MATCHES Clang)
110-
list(APPEND lib_cc_flags
111-
-Wno-c99-extensions
112-
-Wno-gnu-zero-variadic-macro-arguments
113-
-Wno-deprecated-enum-enum-conversion)
114-
115-
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 13.0.0)
116-
list(APPEND lib_cc_flags
117-
-Wno-error=unused-but-set-parameter
118-
-Wno-error=unused-but-set-variable)
119-
endif()
120-
121-
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 17.0.0)
122-
list(APPEND lib_cc_flags
123-
-Wno-vla-cxx-extension
124-
-Wno-error=global-constructors
125-
-Wno-error=shadow)
126-
endif()
127-
128-
elseif(CMAKE_CXX_COMPILER_ID STREQUAL GNU)
129-
list(APPEND lib_cc_flags
130-
-Wmaybe-uninitialized
131-
-Wno-error=unused-but-set-parameter
132-
-Wno-error=unused-but-set-variable
133-
-Wno-deprecated-enum-enum-conversion)
134-
endif()
160+
fbgemm_get_warning_flags(
161+
MSVC_FLAGS_VAR _msvc_flags
162+
CC_FLAGS_VAR _cc_flags
163+
EXTRA_MSVC_FLAGS ${args_MSVC_FLAGS}
164+
EXTRA_CC_FLAGS ${args_CC_FLAGS})
165+
set(lib_cc_flags ${_cc_flags})
135166
endif()
136167

137168
target_compile_options(${lib_name} PRIVATE

cmake/modules/GpuCppLibrary.cmake

Lines changed: 24 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7+
include(${CMAKE_CURRENT_LIST_DIR}/CppLibrary.cmake)
8+
79
function(prepare_target_sources)
810
# This function does the following:
911
#
@@ -35,21 +37,14 @@ function(prepare_target_sources)
3537
############################################################################
3638

3739
# Add the CPU CXX sources
38-
LIST_FILTER(
39-
INPUT ${args_CPU_SRCS}
40-
OUTPUT cpu_sources_cpp
41-
REGEX "^.+\.cpp$"
42-
)
43-
set(${args_PREFIX}_sources_cpp ${cpu_sources_cpp})
40+
set(${args_PREFIX}_sources_cpp ${args_CPU_SRCS})
41+
list(FILTER ${args_PREFIX}_sources_cpp INCLUDE REGEX "^.+\.cpp$")
4442

4543
# For GPU mode, add the CXX sources from GPU_SRCS
4644
if(NOT FBGEMM_BUILD_VARIANT STREQUAL BUILD_VARIANT_CPU)
47-
LIST_FILTER(
48-
INPUT ${args_GPU_SRCS}
49-
OUTPUT gpu_sources_cpp
50-
REGEX "^.+\.cpp$"
51-
)
52-
list(APPEND ${args_PREFIX}_sources_cpp ${gpu_sources_cpp})
45+
set(_gpu_sources_cpp ${args_GPU_SRCS})
46+
list(FILTER _gpu_sources_cpp INCLUDE REGEX "^.+\.cpp$")
47+
list(APPEND ${args_PREFIX}_sources_cpp ${_gpu_sources_cpp})
5348
endif()
5449

5550
# Set source properties
@@ -60,7 +55,7 @@ function(prepare_target_sources)
6055
if(CXX_AVX2_FOUND)
6156
set_source_files_properties(${${args_PREFIX}_sources_cpp}
6257
PROPERTIES COMPILE_OPTIONS
63-
"${AVX2_FLAGS}")
58+
"${CXX_AVX2_FLAGS}")
6459
else()
6560
set_source_files_properties(${${args_PREFIX}_sources_cpp}
6661
PROPERTIES COMPILE_OPTIONS
@@ -76,11 +71,8 @@ function(prepare_target_sources)
7671

7772
if(NOT FBGEMM_BUILD_VARIANT STREQUAL BUILD_VARIANT_CPU)
7873
# Filter GPU_SRCS for CU sources - these may be HIPified later if building in ROCm mode
79-
LIST_FILTER(
80-
INPUT ${args_GPU_SRCS}
81-
OUTPUT ${args_PREFIX}_sources_cu
82-
REGEX "^.+\.cu$"
83-
)
74+
set(${args_PREFIX}_sources_cu ${args_GPU_SRCS})
75+
list(FILTER ${args_PREFIX}_sources_cu INCLUDE REGEX "^.+\.cu$")
8476

8577
# Append CUDA-specific sources, but ONLY when building in CUDA mode
8678
if(NOT FBGEMM_BUILD_VARIANT STREQUAL BUILD_VARIANT_ROCM)
@@ -126,11 +118,8 @@ function(prepare_target_sources)
126118

127119
if(FBGEMM_BUILD_VARIANT STREQUAL BUILD_VARIANT_ROCM)
128120
# Filter GPU_SRCS for HIP sources
129-
LIST_FILTER(
130-
INPUT ${args_GPU_SRCS}
131-
OUTPUT ${args_PREFIX}_sources_hip
132-
REGEX "^.+\.hip$"
133-
)
121+
set(${args_PREFIX}_sources_hip ${args_GPU_SRCS})
122+
list(FILTER ${args_PREFIX}_sources_hip INCLUDE REGEX "^.+\.hip$")
134123

135124
# Append HIP-specific sources, but ONLY when building in HIP mode
136125
list(APPEND ${args_PREFIX}_sources_hip ${args_HIP_SPECIFIC_SRCS})
@@ -281,59 +270,20 @@ function(gpu_cpp_library)
281270
PUBLIC FBGEMM_STATIC)
282271
endif()
283272

284-
set(lib_cc_flags
285-
${args_MSVC_FLAGS}
286-
/wd4244
287-
/wd4267
288-
/wd4305
289-
/wd4309)
273+
fbgemm_get_warning_flags(
274+
MSVC_FLAGS_VAR _msvc_flags
275+
CC_FLAGS_VAR _cc_flags
276+
EXTRA_MSVC_FLAGS ${args_MSVC_FLAGS}
277+
EXTRA_CC_FLAGS ${args_CC_FLAGS})
278+
set(lib_cc_flags ${_msvc_flags})
290279

291280
else()
292-
set(lib_cc_flags
293-
${args_CC_FLAGS}
294-
-Wall
295-
-Wextra
296-
-Werror
297-
-Wunknown-pragmas
298-
-Wimplicit-fallthrough
299-
-Wno-deprecated-enum-enum-conversion
300-
-Wno-deprecated-declarations
301-
-Wno-strict-aliasing
302-
-Wunused-variable
303-
-Wno-sign-compare
304-
-Wno-vla
305-
-Wno-error=unused-parameter
306-
-Wno-error=unknown-pragmas
307-
-Wno-error=attributes)
308-
309-
if(CMAKE_CXX_COMPILER_ID MATCHES Clang)
310-
list(APPEND lib_cc_flags
311-
-Wno-unused-command-line-argument
312-
-Wno-c99-extensions
313-
-Wno-gnu-zero-variadic-macro-arguments
314-
-Wno-deprecated-enum-enum-conversion)
315-
316-
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 13.0.0)
317-
list(APPEND lib_cc_flags
318-
-Wno-error=unused-but-set-parameter
319-
-Wno-error=unused-but-set-variable)
320-
endif()
321-
322-
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 17.0.0)
323-
list(APPEND lib_cc_flags
324-
-Wno-vla-cxx-extension
325-
-Wno-error=global-constructors
326-
-Wno-error=shadow)
327-
endif()
328-
329-
elseif(CMAKE_CXX_COMPILER_ID STREQUAL GNU)
330-
list(APPEND lib_cc_flags
331-
-Wmaybe-uninitialized
332-
-Wno-error=unused-but-set-parameter
333-
-Wno-error=unused-but-set-variable
334-
-Wno-error=array-bounds
335-
-Wno-deprecated-enum-enum-conversion)
336-
endif()
281+
fbgemm_get_warning_flags(
282+
MSVC_FLAGS_VAR _msvc_flags
283+
CC_FLAGS_VAR _cc_flags
284+
EXTRA_MSVC_FLAGS ${args_MSVC_FLAGS}
285+
EXTRA_CC_FLAGS ${args_CC_FLAGS})
286+
set(lib_cc_flags ${_cc_flags})
337287
endif()
338288

339289

cmake/modules/Utilities.cmake

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,8 @@ function(BLOCK_PRINT)
1919
message("")
2020
endfunction()
2121

22-
function(LIST_FILTER)
23-
set(flags)
24-
set(singleValueArgs OUTPUT REGEX)
25-
set(multiValueArgs INPUT)
26-
27-
cmake_parse_arguments(
28-
args
29-
"${flags}" "${singleValueArgs}" "${multiValueArgs}"
30-
${ARGN})
31-
32-
set(${args_OUTPUT})
33-
34-
foreach(value ${args_INPUT})
35-
if("${value}" MATCHES "${args_REGEX}")
36-
list(APPEND ${args_OUTPUT} ${value})
37-
endif()
38-
endforeach()
39-
40-
set(${args_OUTPUT} ${${args_OUTPUT}} PARENT_SCOPE)
41-
endfunction()
42-
43-
function(prepend_filepaths)
44-
set(flags)
45-
set(singleValueArgs PREFIX OUTPUT)
46-
set(multiValueArgs INPUT)
47-
48-
cmake_parse_arguments(
49-
args
50-
"${flags}" "${singleValueArgs}" "${multiValueArgs}"
51-
${ARGN})
52-
53-
set(${args_OUTPUT})
54-
55-
foreach(filepath ${args_INPUT})
56-
list(APPEND ${args_OUTPUT} "${args_PREFIX}/${filepath}")
57-
endforeach()
58-
59-
set(${args_OUTPUT} ${${args_OUTPUT}} PARENT_SCOPE)
60-
endfunction()
61-
6222
macro(handle_genfiles variable)
63-
prepend_filepaths(
64-
PREFIX ${CMAKE_BINARY_DIR}
65-
INPUT ${${variable}}
66-
OUTPUT ${variable})
23+
list(TRANSFORM ${variable} PREPEND "${CMAKE_BINARY_DIR}/")
6724
endmacro()
6825

6926
macro(handle_genfiles_rocm variable)

0 commit comments

Comments
 (0)