Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Hackathon No.65】Refactor and modernize for emscripten #8985

Merged
merged 6 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,10 @@ lite_option(LITE_WITH_XCODE "when debug in xcode, its ON."
lite_option(LITE_WITH_ARM82_FP16 "when compile with arm v8.2 fp16, it's ON." OFF)
lite_option(LITE_WITH_ARM82_INT8_SDOT "when compile with arm v8.2 int8, it's ON." OFF)
lite_option(LITE_WITH_CODE_META_INFO "include git version in the header file." ON)
lite_option(WITH_NODE_RAW_FS "(Only available when compiling by Emscripten) Whether build with NODERAWFS" OFF)

# Thirdparty
set(THIRD_PARTY_PATH "${CMAKE_BINARY_DIR}/third_party" CACHE STRING
set(THIRD_PARTY_PATH "${PADDLE_BINARY_DIR}/third_party" CACHE STRING
Copy link
Contributor Author

@daquexian daquexian May 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所有的 CMAKE_SOURCE_DIR 改成了正确的 PADDLE_SOURCE_DIR,CMAKE_BINARY_DIR 改成了正确的 PADDLE_BINARY_DIR,否则 paddle-lite 无法作为其它项目的 subdirectory

"A path setting third party libraries download & build directories.")

if(NOT LITE_WITH_ARM)
Expand Down Expand Up @@ -124,7 +125,7 @@ endif()

include_directories("${PADDLE_SOURCE_DIR}")
# the generated header files.
set(LITE_GENERATED_INCLUDE_DIR "${CMAKE_BINARY_DIR}")
set(LITE_GENERATED_INCLUDE_DIR "${PADDLE_BINARY_DIR}")
include_directories("${LITE_GENERATED_INCLUDE_DIR}")

if(LITE_WITH_PYTHON)
Expand All @@ -138,10 +139,10 @@ if(LITE_UPDATE_FBS_HEAD)
else()
include_directories("${PADDLE_SOURCE_DIR}/third-party/flatbuffers/pre-build")
add_custom_target(fbs_headers ${TARGET}
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_SOURCE_DIR}/third-party/flatbuffers/pre-build/framework_generated.h" "${CMAKE_SOURCE_DIR}/lite/model_parser/flatbuffers/"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_SOURCE_DIR}/third-party/flatbuffers/pre-build/param_generated.h" "${CMAKE_SOURCE_DIR}/lite/model_parser/flatbuffers/"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_SOURCE_DIR}/third-party/flatbuffers/pre-build/cache_generated.h" "${CMAKE_SOURCE_DIR}/lite/backends/opencl/utils/"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_SOURCE_DIR}/third-party/flatbuffers/pre-build/tune_cache_generated.h" "${CMAKE_SOURCE_DIR}/lite/backends/opencl/utils/"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${PADDLE_SOURCE_DIR}/third-party/flatbuffers/pre-build/framework_generated.h" "${PADDLE_SOURCE_DIR}/lite/model_parser/flatbuffers/"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${PADDLE_SOURCE_DIR}/third-party/flatbuffers/pre-build/param_generated.h" "${PADDLE_SOURCE_DIR}/lite/model_parser/flatbuffers/"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${PADDLE_SOURCE_DIR}/third-party/flatbuffers/pre-build/cache_generated.h" "${PADDLE_SOURCE_DIR}/lite/backends/opencl/utils/"
COMMAND ${CMAKE_COMMAND} -E copy_if_different "${PADDLE_SOURCE_DIR}/third-party/flatbuffers/pre-build/tune_cache_generated.h" "${PADDLE_SOURCE_DIR}/lite/backends/opencl/utils/"
)
endif()

Expand Down
2 changes: 1 addition & 1 deletion cmake/backends/x86.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

if(NOT LITE_WITH_ARM)
if(LITE_WITH_X86)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个属于 bugfix

include(external/xbyak) # download xbyak package
include(external/xxhash) # download install xxhash
include(external/libxsmm) # download, build, install libxsmm
Expand Down
4 changes: 4 additions & 0 deletions cmake/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,7 @@ if (LITE_WITH_M1)
add_definitions("-DLITE_WITH_M1")
endif(LITE_WITH_M1)

if (EMSCRIPTEN)
add_compile_options("-pthread")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
endif()
4 changes: 2 additions & 2 deletions cmake/coverallsGcovJsons.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ if (GIT_FOUND)
# Branch.
execute_process(
COMMAND ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}
OUTPUT_VARIABLE GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE
)

macro (git_log_format FORMAT_CHARS VAR_NAME)
execute_process(
COMMAND ${GIT_EXECUTABLE} log -1 --pretty=format:%${FORMAT_CHARS}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
WORKING_DIRECTORY ${PADDLE_SOURCE_DIR}
OUTPUT_VARIABLE ${VAR_NAME}
OUTPUT_STRIP_TRAILING_WHITESPACE
)
Expand Down
2 changes: 1 addition & 1 deletion cmake/external/eigen.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
INCLUDE(ExternalProject)

SET(EIGEN_SOURCECODE_DIR ${CMAKE_SOURCE_DIR}/third-party/eigen3)
SET(EIGEN_SOURCECODE_DIR ${PADDLE_SOURCE_DIR}/third-party/eigen3)
SET(EIGEN_SOURCE_DIR ${THIRD_PARTY_PATH}/eigen3)
SET(EIGEN_INCLUDE_DIR ${EIGEN_SOURCE_DIR}/src/extern_eigen3)
INCLUDE_DIRECTORIES(${EIGEN_INCLUDE_DIR})
Expand Down
12 changes: 11 additions & 1 deletion cmake/external/gflags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

if (NOT EMSCRIPTEN)
INCLUDE(ExternalProject)

SET(GFLAGS_SOURCES_DIR ${CMAKE_SOURCE_DIR}/third-party/gflags)
SET(GFLAGS_SOURCES_DIR ${PADDLE_SOURCE_DIR}/third-party/gflags)
SET(GFLAGS_INSTALL_DIR ${THIRD_PARTY_PATH}/install/gflags)
SET(GFLAGS_INCLUDE_DIR "${GFLAGS_INSTALL_DIR}/include" CACHE PATH "gflags include directory." FORCE)
IF(WIN32)
Expand Down Expand Up @@ -73,3 +74,12 @@ if (WIN32)
set_property(GLOBAL PROPERTY OS_DEPENDENCY_MODULES shlwapi.lib)
endif(HAVE_SHLWAPI)
endif (WIN32)

else()

option(BUILD_STATIC_LIBS "" ON)
option(BUILD_TESTING "" OFF)
set(GFLAGS_NAMESPACE "google;gflags")
add_subdirectory(${PROJECT_SOURCE_DIR}/third-party/gflags)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样改动有两个原因

  1. cmake ExternalProject_Add 不知道为什么和 emscripten 不兼容
  2. ExternalProject_Add 本身也是很古代的、不应该再使用的 API 了,对 cmake 子项目应该用 add_subdirectory 或者 FetchContent。例如:https://cliutils.gitlab.io/modern-cmake/chapters/projects.html


endif(NOT EMSCRIPTEN)
10 changes: 9 additions & 1 deletion cmake/external/glog.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

if (NOT EMSCRIPTEN)
INCLUDE(ExternalProject)

SET(GLOG_SOURCES_DIR ${CMAKE_SOURCE_DIR}/third-party/glog)
SET(GLOG_SOURCES_DIR ${PADDLE_SOURCE_DIR}/third-party/glog)
SET(GLOG_INSTALL_DIR ${THIRD_PARTY_PATH}/install/glog)
SET(GLOG_INCLUDE_DIR "${GLOG_INSTALL_DIR}/include" CACHE PATH "glog include directory." FORCE)

Expand Down Expand Up @@ -76,3 +77,10 @@ SET_PROPERTY(TARGET glog PROPERTY IMPORTED_LOCATION ${GLOG_LIBRARIES})
ADD_DEPENDENCIES(glog extern_glog)
ADD_DEPENDENCIES(extern_glog extern_gflags)
LINK_LIBRARIES(glog gflags)

else()

set(BUILD_TESTING OFF)
add_subdirectory(${PROJECT_SOURCE_DIR}/third-party/glog)

endif(NOT EMSCRIPTEN)
2 changes: 1 addition & 1 deletion cmake/external/gtest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ IF(WITH_TESTING)

INCLUDE(ExternalProject)

SET(GTEST_SOURCES_DIR ${CMAKE_SOURCE_DIR}/third-party/googletest)
SET(GTEST_SOURCES_DIR ${PADDLE_SOURCE_DIR}/third-party/googletest)
SET(GTEST_INSTALL_DIR ${THIRD_PARTY_PATH}/install/gtest)
SET(GTEST_INCLUDE_DIR "${GTEST_INSTALL_DIR}/include" CACHE PATH "gtest include directory." FORCE)

Expand Down
14 changes: 12 additions & 2 deletions cmake/external/protobuf.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

if (NOT EMSCRIPTEN)
INCLUDE(ExternalProject)
# Always invoke `FIND_PACKAGE(Protobuf)` for importing function protobuf_generate_cpp
IF(NOT WIN32)
Expand Down Expand Up @@ -181,7 +182,7 @@ FUNCTION(build_protobuf TARGET_NAME BUILD_FOR_HOST)
SET(PROTOBUF_TAG "9f75c5aa851cd877fb0d93ccc31b8567a6706546")
SET(OPTIONAL_CACHE_ARGS "")
SET(OPTIONAL_ARGS "")
SET(SOURCE_DIR "${CMAKE_SOURCE_DIR}/third-party/protobuf-host")
SET(SOURCE_DIR "${PADDLE_SOURCE_DIR}/third-party/protobuf-host")

IF(BUILD_FOR_HOST)
# set for server compile.
Expand All @@ -201,7 +202,7 @@ FUNCTION(build_protobuf TARGET_NAME BUILD_FOR_HOST)
# https://github.com/tensor-tang/protobuf.git
SET(PROTOBUF_REPO "")
SET(PROTOBUF_TAG "mobile")
SET(SOURCE_DIR "${CMAKE_SOURCE_DIR}/third-party/protobuf-mobile")
SET(SOURCE_DIR "${PADDLE_SOURCE_DIR}/third-party/protobuf-mobile")
SET(OPTIONAL_ARGS "-Dprotobuf_WITH_ZLIB=OFF"
${CROSS_COMPILE_CMAKE_ARGS}
"-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}"
Expand Down Expand Up @@ -309,3 +310,12 @@ IF(NOT PROTOBUF_FOUND)
ENDIF()

ENDIF(NOT PROTOBUF_FOUND)

else()
option(protobuf_BUILD_TESTS "" OFF)
option(protobuf_WITH_ZLIB "" OFF)
option(protobuf_BUILD_LIBPROTOC "" OFF)
option(protobuf_BUILD_PROTOC_BINARIES "" OFF)
add_subdirectory(${PROJECT_SOURCE_DIR}/third-party/protobuf-host/cmake)
add_library(protobuf ALIAS libprotobuf)
endif(NOT EMSCRIPTEN)
2 changes: 1 addition & 1 deletion cmake/external/pybind11.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ endif()

include(ExternalProject)

set(PYBIND_SOURCE_DIR ${CMAKE_SOURCE_DIR}/third-party/pybind)
set(PYBIND_SOURCE_DIR ${PADDLE_SOURCE_DIR}/third-party/pybind)

include_directories(${PYBIND_SOURCE_DIR}/include)

Expand Down
2 changes: 1 addition & 1 deletion cmake/external/xbyak.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ set(WITH_XBYAK ON)

include(ExternalProject)

SET(XBYAK_SOURCECODE_DIR ${CMAKE_SOURCE_DIR}/third-party/xbyak)
SET(XBYAK_SOURCECODE_DIR ${PADDLE_SOURCE_DIR}/third-party/xbyak)
set(XBYAK_PROJECT extern_xbyak)
set(XBYAK_PREFIX_DIR ${THIRD_PARTY_PATH}/xbyak)
set(XBYAK_INSTALL_ROOT ${THIRD_PARTY_PATH}/install/xbyak)
Expand Down
2 changes: 1 addition & 1 deletion cmake/external/xxhash.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
INCLUDE(ExternalProject)

SET(XXHASH_SOURCECODE_DIR ${CMAKE_SOURCE_DIR}/third-party/xxhash)
SET(XXHASH_SOURCECODE_DIR ${PADDLE_SOURCE_DIR}/third-party/xxhash)
set(XXHASH_SOURCE_DIR ${THIRD_PARTY_PATH}/xxhash)
set(XXHASH_INSTALL_DIR ${THIRD_PARTY_PATH}/install/xxhash)
set(XXHASH_INCLUDE_DIR "${XXHASH_INSTALL_DIR}/include")
Expand Down
8 changes: 6 additions & 2 deletions cmake/flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ if (NOT WIN32)
set(COMMON_FLAGS
-fPIC
-fno-omit-frame-pointer
-Werror
-Wall
-Wextra
-Wnon-virtual-dtor
Expand All @@ -140,6 +139,11 @@ set(COMMON_FLAGS
-Wno-error=maybe-uninitialized # Warning in boost gcc 7.2
)

if (NOT EMSCRIPTEN)
# disable -Werror for Emscripten
set(COMMON_FLAGS "${COMMON_FLAGS} -Werror")
Copy link
Contributor Author

@daquexian daquexian May 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-Werror 会对 emscripten 链接器也生效,而且没办法用 -Wno-error=xxx 来 suppress,所以在使用 emscripten 时就关掉了

endif(EMSCRIPTEN)

set(GPU_COMMON_FLAGS
-fPIC
-fno-omit-frame-pointer
Expand All @@ -154,7 +158,7 @@ set(GPU_COMMON_FLAGS
-Wno-error=array-bounds # Warnings in Eigen::array
-gencode arch=compute_62,code=sm_62
)
if(NOT LITE_WITH_CUDA AND NOT LITE_WITH_SW)
if(NOT LITE_WITH_CUDA AND NOT LITE_WITH_SW AND NOT EMSCRIPTEN)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
endif()
endif(NOT WIN32)
Expand Down
14 changes: 7 additions & 7 deletions cmake/functions.cmake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set(ops_src_list "${CMAKE_BINARY_DIR}/ops_src_list.txt")
set(ops_src_list "${PADDLE_BINARY_DIR}/ops_src_list.txt")
set(OPS_SRC CACHE INTERNAL "")
file(WRITE ${ops_src_list} "") # clean
if(LITE_BUILD_TAILOR)
Expand Down Expand Up @@ -36,11 +36,11 @@ function(add_operator TARGET level)
endfunction()


set(kernels_src_list "${CMAKE_BINARY_DIR}/kernels_src_list.txt")
set(kernels_src_list "${PADDLE_BINARY_DIR}/kernels_src_list.txt")
file(WRITE ${kernels_src_list} "") # clean

# file to record faked kernels for opt python lib
set(fake_kernels_src_list "${CMAKE_BINARY_DIR}/fake_kernels_src_list.txt")
set(fake_kernels_src_list "${PADDLE_BINARY_DIR}/fake_kernels_src_list.txt")
file(WRITE ${fake_kernels_src_list} "") # clean

# add a kernel for some specific device
Expand Down Expand Up @@ -73,7 +73,7 @@ function(add_kernel TARGET device level)
foreach(src ${args_SRCS})
string(TOLOWER "${device}" device_name) # ARM => arm, Host => host
get_filename_component(filename ${src} NAME_WE) # conv_compute.cc => conv_compute
set(kernel_tailor_src_dir "${CMAKE_BINARY_DIR}/kernel_tailor_src_dir")
set(kernel_tailor_src_dir "${PADDLE_BINARY_DIR}/kernel_tailor_src_dir")
set(suffix "for_strip")
set(src_file "${kernel_tailor_src_dir}/${filename}_${device_name}_${suffix}.cc") # conv_compute_arm.cc
if("${device}" STREQUAL "METAL")
Expand Down Expand Up @@ -115,7 +115,7 @@ function(add_kernel TARGET device level)
endfunction()

# Add a unit-test name to file for latter offline manual test.
set(offline_test_registry_file "${CMAKE_BINARY_DIR}/lite_tests.txt")
set(offline_test_registry_file "${PADDLE_BINARY_DIR}/lite_tests.txt")
file(WRITE ${offline_test_registry_file} "") # clean

function(lite_cc_test TARGET)
Expand Down Expand Up @@ -164,9 +164,9 @@ function(lite_cc_test TARGET)
file(APPEND ${offline_test_registry_file} "${TARGET}\n")
add_dependencies(${TARGET} bundle_full_api)
if(NOT WIN32)
target_link_libraries(${TARGET} ${CMAKE_BINARY_DIR}/libpaddle_api_full_bundled.a)
target_link_libraries(${TARGET} ${PADDLE_BINARY_DIR}/libpaddle_api_full_bundled.a)
else()
target_link_libraries(${TARGET} ${CMAKE_BINARY_DIR}/lite/api/${CMAKE_BUILD_TYPE}/libpaddle_api_full_bundled.lib)
target_link_libraries(${TARGET} ${PADDLE_BINARY_DIR}/lite/api/${CMAKE_BUILD_TYPE}/libpaddle_api_full_bundled.lib)
endif()
# windows
if(NOT WIN32)
Expand Down
8 changes: 6 additions & 2 deletions cmake/generic.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ function(cc_library TARGET_NAME)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${source}.h)
list(APPEND cc_library_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/${source}.h)
endif()
if(${source_file} MATCHES ${CMAKE_SOURCE_DIR} AND NOT ${source_file} MATCHES "framework.pb.cc")
if(${source_file} MATCHES ${PADDLE_SOURCE_DIR} AND NOT ${source_file} MATCHES "framework.pb.cc")
list(APPEND full_path_src ${source_file})
elseif( NOT ${source_file} MATCHES "framework.pb.cc")
list(APPEND full_path_src ${CMAKE_CURRENT_SOURCE_DIR}/${source_file})
Expand Down Expand Up @@ -455,6 +455,10 @@ function(paddle_protobuf_generate_cpp SRCS HDRS)
set(${SRCS})
set(${HDRS})

if (NOT EMSCRIPTEN)
set(EXTRA_DEPENDENCY protoc)
endif()

foreach(FIL ${ARGN})
get_filename_component(ABS_FIL ${FIL} ABSOLUTE)
get_filename_component(FIL_WE ${FIL} NAME_WE)
Expand All @@ -472,7 +476,7 @@ function(paddle_protobuf_generate_cpp SRCS HDRS)
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
-I${CMAKE_CURRENT_SOURCE_DIR}
--cpp_out "${CMAKE_CURRENT_BINARY_DIR}" ${ABS_FIL}
DEPENDS ${ABS_FIL} protoc
DEPENDS ${ABS_FIL} ${EXTRA_DEPENDENCY}
COMMENT "Running C++ protocol buffer compiler on ${FIL}"
VERBATIM )
endforeach()
Expand Down
26 changes: 13 additions & 13 deletions cmake/lite.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ add_custom_target(lite_compile_deps COMMAND echo 1)

# Add names for lite libraries for latter compile. We use this name list to avoid compiling
# the whole fluid project to accelerate the compile speed.
set(offline_lib_registry_file "${CMAKE_BINARY_DIR}/lite_libs.txt")
set(offline_lib_registry_file "${PADDLE_BINARY_DIR}/lite_libs.txt")
file(WRITE ${offline_lib_registry_file} "") # clean

# cc_library with branch support.
Expand Down Expand Up @@ -249,14 +249,14 @@ function(lite_cc_binary TARGET)
add_dependencies(${TARGET} bundle_full_api)

if(NOT WIN32)
target_link_libraries(${TARGET} ${CMAKE_BINARY_DIR}/libpaddle_api_full_bundled.a)
target_link_libraries(${TARGET} ${PADDLE_BINARY_DIR}/libpaddle_api_full_bundled.a)
target_compile_options(${TARGET} BEFORE PRIVATE -Wno-ignored-qualifiers)
# openmp dynamic lib is required for mkl
if(WITH_STATIC_MKL)
target_link_libraries(${TARGET} -L${MKLML_LIB_DIR} -liomp5)
endif()
else()
target_link_libraries(${TARGET} ${CMAKE_BINARY_DIR}/lite/api/${CMAKE_BUILD_TYPE}/libpaddle_api_full_bundled.lib)
target_link_libraries(${TARGET} ${PADDLE_BINARY_DIR}/lite/api/${CMAKE_BUILD_TYPE}/libpaddle_api_full_bundled.lib)
endif()

# link to dynamic runtime lib
Expand All @@ -277,7 +277,7 @@ function(lite_cc_binary TARGET)
target_link_libraries(${TARGET} ${intel_fpga_deps})
endif()

if (NOT APPLE AND NOT WIN32)
if (NOT APPLE AND NOT WIN32 AND NOT EMSCRIPTEN)
# strip binary target to reduce size
if(NOT "${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
add_custom_command(TARGET ${TARGET} POST_BUILD
Expand Down Expand Up @@ -356,9 +356,9 @@ function(bundle_static_library tgt_name bundled_tgt_name fake_target)
list(REMOVE_DUPLICATES static_libs)

set(bundled_tgt_full_name
${CMAKE_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${bundled_tgt_name}${CMAKE_STATIC_LIBRARY_SUFFIX})
${PADDLE_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${bundled_tgt_name}${CMAKE_STATIC_LIBRARY_SUFFIX})

message(STATUS "bundled_tgt_full_name: ${CMAKE_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${bundled_tgt_name}${CMAKE_STATIC_LIBRARY_SUFFIX}")
message(STATUS "bundled_tgt_full_name: ${PADDLE_BINARY_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${bundled_tgt_name}${CMAKE_STATIC_LIBRARY_SUFFIX}")

if(WIN32)
set(dummy_tgt_name dummy_${bundled_tgt_name})
Expand All @@ -379,20 +379,20 @@ function(bundle_static_library tgt_name bundled_tgt_name fake_target)
add_dependencies(${fake_target} ${tgt_name})

if(NOT IOS AND NOT APPLE)
file(WRITE ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar.in
file(WRITE ${PADDLE_BINARY_DIR}/${bundled_tgt_name}.ar.in
"CREATE ${bundled_tgt_full_name}\n" )

foreach(tgt IN LISTS static_libs)
file(APPEND ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar.in
file(APPEND ${PADDLE_BINARY_DIR}/${bundled_tgt_name}.ar.in
"ADDLIB $<TARGET_FILE:${tgt}>\n")
endforeach()

file(APPEND ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar.in "SAVE\n")
file(APPEND ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar.in "END\n")
file(APPEND ${PADDLE_BINARY_DIR}/${bundled_tgt_name}.ar.in "SAVE\n")
file(APPEND ${PADDLE_BINARY_DIR}/${bundled_tgt_name}.ar.in "END\n")

file(GENERATE
OUTPUT ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar
INPUT ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar.in)
OUTPUT ${PADDLE_BINARY_DIR}/${bundled_tgt_name}.ar
INPUT ${PADDLE_BINARY_DIR}/${bundled_tgt_name}.ar.in)

set(ar_tool ${CMAKE_AR})
if (CMAKE_INTERPROCEDURAL_OPTIMIZATION)
Expand All @@ -402,7 +402,7 @@ function(bundle_static_library tgt_name bundled_tgt_name fake_target)
add_custom_command(
TARGET ${fake_target} PRE_BUILD
COMMAND rm -f ${bundled_tgt_full_name}
COMMAND ${ar_tool} -M < ${CMAKE_BINARY_DIR}/${bundled_tgt_name}.ar
COMMAND ${ar_tool} -M < ${PADDLE_BINARY_DIR}/${bundled_tgt_name}.ar
COMMENT "Bundling ${bundled_tgt_name}"
DEPENDS ${tgt_name}
VERBATIM)
Expand Down
Loading