Skip to content

Commit 92314c8

Browse files
zdevitofacebook-github-bot
authored andcommitted
re-enable copy of python files, but be careful that the copy is only … (pytorch#14982)
Summary: …done once This allow no-op build to work correctly even when BUILD_CAFFE2_OPS is on. Pull Request resolved: pytorch#14982 Differential Revision: D13413960 Pulled By: zdevito fbshipit-source-id: 6e5412a8c375af8a47c76f548cdd31cff15f3853
1 parent 71e0cb5 commit 92314c8

File tree

15 files changed

+19
-79
lines changed

15 files changed

+19
-79
lines changed

caffe2/CMakeLists.txt

+14-29
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ endif()
471471
# rebuild step. The long-term fix should be to clean up these rules so they
472472
# only rerun when needed.
473473

474-
if (BUILD_PYTHON AND BUILD_CAFFE2_OPS)
474+
if (BUILD_PYTHON)
475475
# Python site-packages
476476
# Get canonical directory for python site packages (relative to install
477477
# location). It varies from system to system.
@@ -628,37 +628,22 @@ if (BUILD_PYTHON AND BUILD_CAFFE2_OPS)
628628
endif()
629629

630630
# Finally, Copy all python files to build directory
631-
# Generate and create all needed __init__.py files, if they aren't already
632-
# present in the current source tree.
633-
message(STATUS "Automatically generating missing __init__.py files.")
634-
caffe_autogen_init_py_files()
635-
636631
# Create a custom target that copies all python files.
637632
file(GLOB_RECURSE PYTHON_SRCS RELATIVE ${PROJECT_SOURCE_DIR}
638633
"${PROJECT_SOURCE_DIR}/caffe2/*.py")
639-
add_custom_target(python_copy_files ALL)
640-
if(MSVC OR CMAKE_GENERATOR MATCHES "Ninja")
641-
# ninja fails when the command line is too long so we split
642-
# the target into several. This would be beneficial for VS also
643-
# since it build targets in parallel but not custom commands
644-
foreach(python_src ${PYTHON_SRCS})
645-
get_filename_component(dir ${python_src} DIRECTORY)
646-
string(SHA1 name_hash "${python_src}")
647-
# get_filename_component(name_we ${python_src} NAME_WE)
648-
add_custom_target(python_copy_files_${name_hash}
649-
COMMAND ${CMAKE_COMMAND} -E copy
650-
${PROJECT_SOURCE_DIR}/${python_src} ${CMAKE_BINARY_DIR}/${dir})
651-
add_dependencies(python_copy_files python_copy_files_${name_hash})
652-
endforeach()
653-
else()
654-
foreach(python_src ${PYTHON_SRCS})
655-
get_filename_component(dir ${python_src} DIRECTORY)
656-
add_custom_command(
657-
TARGET python_copy_files PRE_BUILD
658-
COMMAND ${CMAKE_COMMAND} -E copy
659-
${PROJECT_SOURCE_DIR}/${python_src} ${CMAKE_BINARY_DIR}/${dir})
660-
endforeach()
661-
endif()
634+
635+
set(build_files)
636+
foreach(python_src ${PYTHON_SRCS})
637+
add_custom_command(OUTPUT ${CMAKE_BINARY_DIR}/${python_src}
638+
DEPENDS ${PROJECT_SOURCE_DIR}/${python_src}
639+
COMMAND ${CMAKE_COMMAND} -E copy
640+
${PROJECT_SOURCE_DIR}/${python_src}
641+
${CMAKE_BINARY_DIR}/${python_src})
642+
list(APPEND build_files ${CMAKE_BINARY_DIR}/${python_src})
643+
endforeach()
644+
645+
add_custom_target(python_copy_files ALL DEPENDS ${build_files})
646+
662647

663648
# Install commands
664649
# Pick up static python files

caffe2/contrib/aten/docs/__init__.py

Whitespace-only changes.

caffe2/contrib/cuda-convnet2/make-data/__init__.py

Whitespace-only changes.

caffe2/contrib/script/examples/__init__.py

Whitespace-only changes.

caffe2/core/__init__.py

Whitespace-only changes.

caffe2/core/nomnigraph/__init__.py

Whitespace-only changes.

caffe2/experiments/__init__.py

Whitespace-only changes.

caffe2/experiments/python/__init__.py

Whitespace-only changes.

caffe2/perfkernels/__init__.py

Whitespace-only changes.

caffe2/python/mint/__init__.py

Whitespace-only changes.

caffe2/quantization/__init__.py

Whitespace-only changes.

caffe2/quantization/server/__init__.py

Whitespace-only changes.

cmake/Utils.cmake

-36
Original file line numberDiff line numberDiff line change
@@ -128,42 +128,6 @@ function(caffe2_parse_version_str LIBNAME VERSIONSTR)
128128
set(${LIBNAME}_VERSION "${${LIBNAME}_VERSION_MAJOR}.${${LIBNAME}_VERSION_MINOR}.${${LIBNAME}_VERSION_PATCH}" PARENT_SCOPE)
129129
endfunction()
130130

131-
##############################################################################
132-
# Helper function to automatically generate __init__.py files where python
133-
# sources reside but there are no __init__.py present.
134-
function(caffe_autogen_init_py_files)
135-
if (CAFFE2_AUTOGEN_INIT_PY_ALREADY_RUN)
136-
message(STATUS
137-
"A previous caffe2 cmake run already created the __init__.py files.")
138-
return()
139-
endif()
140-
file(GLOB_RECURSE all_python_files RELATIVE ${PROJECT_SOURCE_DIR}
141-
"${PROJECT_SOURCE_DIR}/caffe2/*.py")
142-
set(python_paths_need_init_py)
143-
foreach(python_file ${all_python_files})
144-
get_filename_component(python_path ${python_file} PATH)
145-
string(REPLACE "/" ";" path_parts ${python_path})
146-
set(rebuilt_path ${CMAKE_BINARY_DIR})
147-
foreach(path_part ${path_parts})
148-
set(rebuilt_path "${rebuilt_path}/${path_part}")
149-
list(APPEND python_paths_need_init_py ${rebuilt_path})
150-
endforeach()
151-
endforeach()
152-
list(REMOVE_DUPLICATES python_paths_need_init_py)
153-
# Since the _pb2.py files are yet to be created, we will need to manually
154-
# add them to the list.
155-
list(APPEND python_paths_need_init_py ${CMAKE_BINARY_DIR}/caffe2/proto)
156-
157-
foreach(tmp ${python_paths_need_init_py})
158-
if(NOT EXISTS ${tmp}/__init__.py)
159-
# message(STATUS "Generate " ${tmp}/__init__.py)
160-
file(WRITE ${tmp}/__init__.py "")
161-
endif()
162-
endforeach()
163-
set(CAFFE2_AUTOGEN_INIT_PY_ALREADY_RUN TRUE CACHE INTERNAL
164-
"Helper variable to record if autogen_init_py is already run or not.")
165-
endfunction()
166-
167131
###
168132
# Removes common indentation from a block of text to produce code suitable for
169133
# setting to `python -c`, or using with pycmd. This allows multiline code to be

setup.py

-13
Original file line numberDiff line numberDiff line change
@@ -425,18 +425,6 @@ def build_libs(libs):
425425
sys.exit(1)
426426

427427

428-
# Copy Caffe2's Python proto files (generated during the build with the
429-
# protobuf python compiler) from the build folder to the root folder
430-
# cp root/build/caffe2/proto/proto.py root/caffe2/proto/proto.py
431-
def copy_protos():
432-
report('setup.py::copy_protos()')
433-
for src in glob.glob(
434-
os.path.join(caffe2_build_dir, 'caffe2', 'proto', '*.py')):
435-
dst = os.path.join(
436-
cwd, os.path.relpath(src, caffe2_build_dir))
437-
shutil.copyfile(src, dst)
438-
439-
440428
# Build all dependent libraries
441429
class build_deps(PytorchCommand):
442430
def run(self):
@@ -528,7 +516,6 @@ def run(self):
528516
self.run_command('create_version_file')
529517
setuptools.command.develop.develop.run(self)
530518
self.create_compile_commands()
531-
copy_protos()
532519

533520
def create_compile_commands(self):
534521
def load(filename):

tools/build_pytorch_libs.sh

+5-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,11 @@ function build_caffe2() {
287287
fi
288288

289289
for proto_file in $(pwd)/caffe2/proto/*.py; do
290-
cp $proto_file "$(pwd)/../caffe2/proto/"
290+
# __init__.py is not auto-generated, copying it breaks
291+
# mod-times for rebuild logic
292+
if [[ "$proto_file" != "$(pwd)/caffe2/proto/__init__.py" ]]; then
293+
cp $proto_file "$(pwd)/../caffe2/proto/"
294+
fi
291295
done
292296
fi
293297

0 commit comments

Comments
 (0)