Skip to content

Commit 9291f9b

Browse files
cyyeverpytorchmergebot
authored andcommitted
Simplify cmake code (pytorch#91546)
We use various newer CMake features to simplify build system: 1.Caffe2::threads is replaced by threads::threads. 2.Some unused MSVC flags are removed. Pull Request resolved: pytorch#91546 Approved by: https://github.com/malfet, https://github.com/Skylion007
1 parent c981b7e commit 9291f9b

8 files changed

+13
-145
lines changed

CMakeLists.txt

-9
Original file line numberDiff line numberDiff line change
@@ -477,14 +477,6 @@ if(MSVC)
477477
# Turn off warnings on Windows. In an ideal world we'd be warning
478478
# clean on Windows too, but this is too much work for our
479479
# non-Windows developers.
480-
#
481-
# NB: Technically, this is not necessary if CMP0092 was applied
482-
# properly, but only cmake >= 3.15 has this policy, so we nail
483-
# it one more time just be safe.
484-
#
485-
# NB2: This is NOT enough to prevent warnings from nvcc on MSVC. At the
486-
# moment only CMP0092 is enough to prevent those warnings too.
487-
string(REPLACE "/W3" "" ${flag_var} "${${flag_var}}")
488480

489481
# Turn off warnings (Windows build is currently is extremely warning
490482
# unclean and the warnings aren't telling us anything useful.)
@@ -1120,7 +1112,6 @@ if(BUILD_SHARED_LIBS)
11201112
${PROJECT_SOURCE_DIR}/cmake/public/mkl.cmake
11211113
${PROJECT_SOURCE_DIR}/cmake/public/mkldnn.cmake
11221114
${PROJECT_SOURCE_DIR}/cmake/public/protobuf.cmake
1123-
${PROJECT_SOURCE_DIR}/cmake/public/threads.cmake
11241115
${PROJECT_SOURCE_DIR}/cmake/public/utils.cmake
11251116
${PROJECT_SOURCE_DIR}/cmake/public/LoadHIP.cmake
11261117
DESTINATION share/cmake/Caffe2/public

cmake/Caffe2Config.cmake.in

-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ set(CAFFE2_VERSION "@CAFFE2_VERSION@")
1313
# Utils functions.
1414
include("${CMAKE_CURRENT_LIST_DIR}/public/utils.cmake")
1515

16-
# Include threads lib.
17-
include("${CMAKE_CURRENT_LIST_DIR}/public/threads.cmake")
18-
1916
# Depending on whether Caffe2 uses gflags during compile time or
2017
# not, invoke gflags.
2118
if(@USE_GFLAGS@)

cmake/Dependencies.cmake

+10-6
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ if(MSVC)
124124
endif(MSVC)
125125

126126
# ---[ Threads
127-
include(${CMAKE_CURRENT_LIST_DIR}/public/threads.cmake)
128-
if(TARGET caffe2::Threads)
129-
list(APPEND Caffe2_PUBLIC_DEPENDENCY_LIBS caffe2::Threads)
127+
find_package(Threads REQUIRED)
128+
if(TARGET Threads::Threads)
129+
list(APPEND Caffe2_DEPENDENCY_LIBS Threads::Threads)
130+
add_library(caffe2::Threads ALIAS Threads::Threads)
130131
else()
131132
message(FATAL_ERROR
132133
"Cannot find threading library. Caffe2 requires Threads to compile.")
@@ -1080,17 +1081,17 @@ if(BUILD_PYTHON)
10801081

10811082
# These should fill in the rest of the variables, like versions, but resepct
10821083
# the variables we set above
1083-
set(Python_ADDITIONAL_VERSIONS ${PYTHON_VERSION} 3.8 3.7)
1084+
set(Python_ADDITIONAL_VERSIONS ${PYTHON_VERSION} 3.8)
10841085
find_package(PythonInterp 3.0)
10851086
find_package(PythonLibs 3.0)
10861087

10871088
if(${PYTHONLIBS_VERSION_STRING} VERSION_LESS 3)
10881089
message(FATAL_ERROR
10891090
"Found Python libraries version ${PYTHONLIBS_VERSION_STRING}. Python 2 has reached end-of-life and is no longer supported by PyTorch.")
10901091
endif()
1091-
if(${PYTHONLIBS_VERSION_STRING} VERSION_LESS 3.7)
1092+
if(${PYTHONLIBS_VERSION_STRING} VERSION_LESS 3.8)
10921093
message(FATAL_ERROR
1093-
"Found Python libraries version ${PYTHONLIBS_VERSION_STRING}. Python 3.6 is no longer supported by PyTorch.")
1094+
"Found Python libraries version ${PYTHONLIBS_VERSION_STRING}. Python < 3.8 is no longer supported by PyTorch.")
10941095
endif()
10951096

10961097
# When building pytorch, we pass this in directly from setup.py, and
@@ -1145,6 +1146,9 @@ message(STATUS "pybind11 include dirs: " "${pybind11_INCLUDE_DIRS}")
11451146
add_library(pybind::pybind11 INTERFACE IMPORTED)
11461147
target_include_directories(pybind::pybind11 SYSTEM INTERFACE ${pybind11_INCLUDE_DIRS})
11471148
target_link_libraries(pybind::pybind11 INTERFACE python::python)
1149+
if(APPLE)
1150+
target_link_options(pybind::pybind11 INTERFACE -undefined dynamic_lookup)
1151+
endif()
11481152

11491153
# ---[ MPI
11501154
if(USE_MPI)

cmake/GoogleTestPatch.cmake

-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,5 @@ else(REVERT)
2020
file(READ ${FILENAME} content)
2121
file(WRITE ${BACKUP} "${content}")
2222
string(REGEX REPLACE "[-/]Z[iI]" "/Z7" content "${content}")
23-
string(REGEX REPLACE "Threads::Threads" "caffe2::Threads" content "${content}")
2423
file(WRITE ${FILENAME} "${content}")
2524
endif(REVERT)

cmake/MiscCheck.cmake

-79
Original file line numberDiff line numberDiff line change
@@ -136,85 +136,6 @@ if(NOT MSVC)
136136
endif()
137137
endif()
138138

139-
# ---[ If we are using msvc, set no warning flags
140-
# Note(jiayq): if you are going to add a warning flag, check if this is
141-
# totally necessary, and only add when you see fit. If it is needed due to
142-
# a third party library (like Protobuf), mention it in the comment as
143-
# "THIRD_PARTY_NAME related"
144-
# From https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/
145-
if(${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC")
146-
add_compile_options(
147-
##########################################
148-
# Protobuf related. Cannot remove.
149-
# This is directly copied from
150-
# https://github.com/google/protobuf/blob/master/cmake/README.md
151-
##########################################
152-
/wd4018 # 'expression' : signed/unsigned mismatch
153-
/wd4065 # (3): switch with default but no case.
154-
/wd4146 # unary minus operator applied to unsigned type, result still unsigned
155-
/wd4244 # Conversion from 'type1' to 'type2', possible loss of data.
156-
/wd4251 # 'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'
157-
/wd4267 # Conversion from 'size_t' to 'type', possible loss of data.
158-
/wd4305 # 'identifier' : truncation from 'type1' to 'type2'
159-
/wd4355 # 'this' : used in base member initializer list
160-
/wd4506 # (1): no definition for inline function. Protobuf related.
161-
/wd4661 # No suitable definition provided for explicit template instantiation request
162-
/wd4800 # 'type' : forcing value to bool 'true' or 'false' (performance warning)
163-
/wd4996 # 'function': was declared deprecated
164-
##########################################
165-
# Third party related. Cannot remove.
166-
##########################################
167-
/wd4141 # (1): inline used twice. google benchmark related.
168-
/wd4503 # (1): decorated name length exceeded, name was truncated.
169-
# Eigen related.
170-
/wd4554 # (3): check operator precedence for possible error.
171-
# Eigen related.
172-
/wd4805 # (1): Unsafe mix of types in gtest/gtest.h. Gtest related.
173-
##########################################
174-
# These are directly ATen related. However, several are covered by
175-
# the above now. We leave them here for documentation purposes only.
176-
#/wd4267 # Conversion from 'size_t' to 'type', possible loss of data.
177-
/wd4522 # (3): 'class' : multiple assignment operators specified
178-
/wd4838 # (1): conversion from 'type_1' to 'type_2' requires a
179-
# narrowing conversion
180-
#/wd4305 # 'identifier' : truncation from 'type1' to 'type2'
181-
#/wd4244 # Conversion from 'type1' to 'type2', possible loss of data.
182-
/wd4190 # (1): 'identifier1' has C-linkage specified, but returns UDT
183-
# 'identifier2' which is incompatible with C
184-
/wd4101 # (3): 'identifier' : unreferenced local variable
185-
#/wd4996 # (3): Use of deprecated POSIX functions. Since we develop
186-
# # mainly on Linux, this is ignored.
187-
/wd4275 # (2): non - DLL-interface classkey 'identifier' used as
188-
# base for DLL-interface classkey 'identifier'
189-
##########################################
190-
# These are directly Caffe2 related. However, several are covered by
191-
# protobuf now. We leave them here for documentation purposes only.
192-
##########################################
193-
#/wd4018 # (3): Signed/unsigned mismatch. We've used it in many places
194-
# # of the code and it would be hard to correct all.
195-
#/wd4244 # (2/3/4): Possible loss of precision. Various cases where we
196-
# # implicitly cast TIndex to int etc. Need cleaning.
197-
#/wd4267 # (3): Conversion of size_t to smaller type. Same as 4244.
198-
#/wd4996 # (3): Use of deprecated POSIX functions. Since we develop
199-
# # mainly on Linux, this is ignored.
200-
/wd4273 # (1): inconsistent dll linkage. This is related to the
201-
# caffe2 FLAGS_* definition using dllimport in header and
202-
# dllexport in cc file. The strategy is copied from gflags.
203-
)
204-
205-
# Make sure windows.h does not include additional headers.
206-
add_definitions("/DWIN32_LEAN_AND_MEAN")
207-
208-
# Make sure windef.h does not define max/min macros.
209-
# Required by ATen among others.
210-
add_definitions("/DNOMINMAX")
211-
212-
set(CMAKE_SHARED_LINKER_FLAGS
213-
"${CMAKE_SHARED_LINKER_FLAGS} /ignore:4049 /ignore:4217 /ignore:4099")
214-
set(CMAKE_EXE_LINKER_FLAGS
215-
"${CMAKE_EXE_LINKER_FLAGS} /ignore:4049 /ignore:4217 /ignore:4099")
216-
endif()
217-
218139
# ---[ If we are building on ios, or building with opengl support, we will
219140
# enable -mfpu=neon-fp16 for iOS Metal build. For Android, this fpu setting
220141
# is going to be done with android-cmake by setting

cmake/public/cuda.cmake

-15
Original file line numberDiff line numberDiff line change
@@ -405,21 +405,6 @@ set_property(
405405
TARGET caffe2::nvrtc PROPERTY INTERFACE_INCLUDE_DIRECTORIES
406406
${CUDA_INCLUDE_DIRS})
407407

408-
# Note: in theory, we can add similar dependent library wrappers. For
409-
# now, Caffe2 only uses the above libraries, so we will only wrap
410-
# these.
411-
412-
# Special care for windows platform: we know that 32-bit windows does not
413-
# support cuda.
414-
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
415-
if(NOT (CMAKE_SIZEOF_VOID_P EQUAL 8))
416-
message(FATAL_ERROR
417-
"CUDA support not available with 32-bit windows. Did you "
418-
"forget to set Win64 in the generator target?")
419-
return()
420-
endif()
421-
endif()
422-
423408
# Add onnx namepsace definition to nvcc
424409
if(ONNX_NAMESPACE)
425410
list(APPEND CUDA_NVCC_FLAGS "-DONNX_NAMESPACE=${ONNX_NAMESPACE}")

cmake/public/threads.cmake

-29
This file was deleted.

torch/lib/libshm/CMakeLists.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ project(libshm C CXX)
22
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
33

44
set(TORCH_ROOT ${CMAKE_CURRENT_LIST_DIR}/../../../)
5-
include(${TORCH_ROOT}/cmake/public/threads.cmake)
65

76
if(NOT LIBSHM_INSTALL_LIB_SUBDIR)
87
set(LIBSHM_INSTALL_LIB_SUBDIR "lib" CACHE PATH "libshm install library directory")
@@ -34,6 +33,7 @@ target_link_libraries(shm PUBLIC torch)
3433

3534
if(UNIX AND NOT APPLE)
3635
include(CheckLibraryExists)
36+
find_package(Threads REQUIRED)
3737
# https://github.com/libgit2/libgit2/issues/2128#issuecomment-35649830
3838
check_library_exists(rt clock_gettime "time.h" NEED_LIBRT)
3939
if(NEED_LIBRT)
@@ -56,12 +56,12 @@ if(UNIX AND NOT APPLE)
5656
# site above though in case there was a reason we were testing
5757
# against clock_gettime. In principle, the choice of symbol you
5858
# test for shouldn't matter.
59-
set(CMAKE_REQUIRED_LIBRARIES caffe2::Threads)
59+
set(CMAKE_REQUIRED_LIBRARIES Threads::Threads)
6060
check_library_exists(rt shm_open "sys/mman.h" NEED_RT_AND_PTHREAD)
6161
unset(CMAKE_REQUIRED_LIBRARIES)
6262
if(NEED_RT_AND_PTHREAD)
6363
message(STATUS "Needs it, linking against pthread and rt")
64-
target_link_libraries(shm PUBLIC rt caffe2::Threads)
64+
target_link_libraries(shm PUBLIC rt Threads::Threads)
6565
endif()
6666
endif()
6767
endif()

0 commit comments

Comments
 (0)