Skip to content

Commit c1d8580

Browse files
puneetmatharuandflo-Arm
authored andcommitted
feat: Modernize ACL CMake build (cont'd)
Summary of changes: + Improved target installation, export, and 'find_package(ArmCompute)' support. + Set 'CMAKE_INSTALL_PREFIX' to <project-root>/build/install/ if unset (only if the project is top-level). + Ensured correct 'RPATH' handling for installed binaries. + Added 'ArmCompute::ArmCompute' alias for easier linking. + Enabled '_d' postfix for debug libraries in multi-config generators. + Updated '.gitignore'. + Allows users to specify compile flags (i.e. it no longer wipes them). Signed-off-by: Puneet Matharu <[email protected]> Change-Id: Iccea661508c5ee41b8e106aaaab630b0819a5f11 Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/13721 Tested-by: Arm Jenkins <[email protected]> Comments-Addressed: Arm Jenkins <[email protected]> Reviewed-by: Andreas Flöjt <[email protected]> Reviewed-by: Hamza <[email protected]> Benchmark: Arm Jenkins <[email protected]>
1 parent a491853 commit c1d8580

File tree

4 files changed

+155
-11
lines changed

4 files changed

+155
-11
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
# SOFTWARE.
2222

2323
# Library builds
24-
build/
24+
/build/
2525
bazel-*
2626

27+
# CMake files
28+
CMakeUserPresets.json
29+
2730
# Cscope/Ctags files
2831
*cscope*
2932

CMakeLists.txt

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ set(ARM_COMPUTE_SVE2_ARCH armv8.6-a+sve2+fp16+dotprod CACHE STRING "Architecture
5959
set(ARM_COMPUTE_C_STANDARD 99 CACHE STRING "C Standard to use for the library.")
6060
set(ARM_COMPUTE_CXX_STANDARD 14 CACHE STRING "CXX Standard to use for the library.")
6161

62+
# If this is the top-level project and the installation directory hasn't been set, set it
63+
# to be <project-root>/install/
64+
if(PROJECT_IS_TOP_LEVEL)
65+
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
66+
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Install path" FORCE)
67+
message(STATUS "CMAKE_INSTALL_PREFIX was not set, using default: ${CMAKE_INSTALL_PREFIX}")
68+
endif()
69+
endif()
70+
6271
include(${CMAKE_CURRENT_LIST_DIR}/cmake/configurations.cmake)
6372
include(${CMAKE_CURRENT_LIST_DIR}/cmake/compilers/setup.cmake)
6473
include(${CMAKE_CURRENT_LIST_DIR}/cmake/version.cmake)
@@ -67,12 +76,15 @@ if(ARM_COMPUTE_ENABLE_OPENMP)
6776
find_package(OpenMP REQUIRED)
6877
endif()
6978

79+
# Introduce CMAKE_INSTALL_BINDIR, CMAKE_INSTALL_LIBDIR, CMAKE_INSTALL_INCLUDEDIR variables
80+
include(GNUInstallDirs)
81+
7082
# Set lib build type accordingly
7183
if(ARM_COMPUTE_BUILD_SHARED_LIB)
72-
message(STATUS "Will build the Arm Compute Library with shared libraries.")
84+
message(STATUS "Building Arm Compute Library with shared libraries.")
7385
set(ARM_COMPUTE_LIB_BUILD_TYPE SHARED)
7486
else()
75-
message(STATUS "Will build the Arm Compute Library with static libraries.")
87+
message(STATUS "Building Arm Compute Library with static libraries.")
7688
set(ARM_COMPUTE_LIB_BUILD_TYPE STATIC)
7789
endif()
7890

@@ -176,12 +188,23 @@ add_library(
176188
# Linking to arm_compute[_graph] should automatically bring includes and dependent libs with it
177189
foreach(TARGET IN ITEMS arm_compute arm_compute_graph)
178190
target_link_libraries(${TARGET} PUBLIC ${ARM_COMPUTE_LINK_LIBS})
179-
target_include_directories(${TARGET} PUBLIC ${ARM_COMPUTE_PUBLIC_INCLUDE})
191+
target_include_directories(${TARGET} PUBLIC
192+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
193+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
194+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
195+
)
180196
set_target_properties(${TARGET} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
181197
endforeach()
182198

199+
# Linking to this target should automatically bring includes and dependent libs with it.
183200
list(APPEND ARM_COMPUTE_TARGETS arm_compute arm_compute_graph arm_compute_core arm_compute_core_fp16 arm_compute_sve arm_compute_sve2)
184201

202+
# Create an alias targets so that a user can download ArmCompute via FetchContent and
203+
# still link to ArmCompute::Core and ArmCompute::Graph. Otherwise these targets would not
204+
# be available at configure-time (as required by FetchContent)
205+
add_library(${PROJECT_NAME}::Core ALIAS arm_compute)
206+
add_library(${PROJECT_NAME}::Graph ALIAS arm_compute_graph)
207+
185208
# Library target sources.
186209
add_subdirectory(src)
187210

@@ -200,6 +223,11 @@ if(ARM_COMPUTE_BUILD_TESTING)
200223
INCLUDE_DIRECTORIES "${ARM_COMPUTE_PUBLIC_INCLUDE};${ARM_COMPUTE_COMMON_INCLUDE}"
201224
COMPILE_DEFINITIONS "${ARM_COMPUTE_DEFINES}"
202225
LINK_LIBRARIES "arm_compute;arm_compute_graph"
226+
227+
# Adjusted for relative location of installed arm_compute lib
228+
INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}"
229+
BUILD_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}"
230+
INSTALL_RPATH_USE_LINK_PATH TRUE
203231
)
204232
list(APPEND ARM_COMPUTE_TARGETS arm_compute_validation arm_compute_benchmark)
205233
endif(ARM_COMPUTE_BUILD_TESTING)
@@ -217,6 +245,10 @@ if(ARM_COMPUTE_BUILD_EXAMPLES)
217245
utils/GraphUtils.cpp
218246
utils/CommonGraphOptions.cpp
219247
)
248+
target_include_directories(${test_name} PRIVATE
249+
$<BUILD_INTERFACE:${ARM_COMPUTE_PUBLIC_INCLUDE}>
250+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
251+
)
220252
endforeach()
221253

222254
# NEON Examples
@@ -226,6 +258,10 @@ if(ARM_COMPUTE_BUILD_EXAMPLES)
226258
"examples/${test_name}.cpp"
227259
utils/Utils.cpp
228260
)
261+
target_include_directories(${test_name} PRIVATE
262+
$<BUILD_INTERFACE:${ARM_COMPUTE_PUBLIC_INCLUDE}>
263+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
264+
)
229265
endforeach()
230266

231267
# Set common properties
@@ -262,11 +298,79 @@ set_target_properties(
262298
)
263299

264300
# Install libraries
265-
include(GNUInstallDirs)
266301
install(
267302
TARGETS
268303
${ARM_COMPUTE_TARGETS}
304+
EXPORT ${PROJECT_NAME}Targets
269305
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
270306
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
271307
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
272308
)
309+
310+
# Introduce the functions write_basic_package_version_file(...) and
311+
# configure_package_config_file(...) into the current workspace
312+
include(CMakePackageConfigHelpers)
313+
314+
# Set filenames and installation destinations
315+
set(ARM_COMPUTE_CONFIG_FILE ${PROJECT_NAME}Config.cmake)
316+
set(ARM_COMPUTE_CONFIG_VERSION_FILE ${PROJECT_NAME}ConfigVersion.cmake)
317+
set(ARM_COMPUTE_TARGETS_NAME ${PROJECT_NAME}Targets)
318+
set(ARM_COMPUTE_TARGETS_FILE "${CMAKE_CURRENT_BINARY_DIR}/${ARM_COMPUTE_TARGETS_NAME}.cmake")
319+
set(ARM_COMPUTE_CONFIG_TEMPLATE_FILE "cmake/${ARM_COMPUTE_CONFIG_FILE}.in")
320+
set(ARM_COMPUTE_CONFIG_OUTPUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/${ARM_COMPUTE_CONFIG_FILE}")
321+
set(ARM_COMPUTE_CONFIG_VERSION_OUTPUT_FILE "${CMAKE_CURRENT_BINARY_DIR}/${ARM_COMPUTE_CONFIG_VERSION_FILE}")
322+
set(ARM_COMPUTE_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
323+
324+
# Add all targets to the build-tree export set
325+
export(
326+
EXPORT ${PROJECT_NAME}Targets
327+
NAMESPACE ${PROJECT_NAME}::
328+
FILE "${ARM_COMPUTE_TARGETS_FILE}"
329+
)
330+
331+
# Install the exported targets to allow other projects to find this project with find_package()
332+
install(
333+
EXPORT ${PROJECT_NAME}Targets
334+
NAMESPACE ${PROJECT_NAME}::
335+
DESTINATION "${ARM_COMPUTE_CONFIG_INSTALL_DIR}"
336+
)
337+
338+
# Install header files
339+
# N.B. The absence of a trailing slash is *important*. Do not add one.
340+
install(
341+
DIRECTORY
342+
"${CMAKE_CURRENT_LIST_DIR}/include/"
343+
"${CMAKE_CURRENT_LIST_DIR}/arm_compute"
344+
"${CMAKE_CURRENT_LIST_DIR}/utils"
345+
"${CMAKE_CURRENT_LIST_DIR}/support"
346+
"${CMAKE_CURRENT_LIST_DIR}/third_party/kleidiai"
347+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
348+
)
349+
350+
# Install the header files from the src/ directory (only) preserving the folder structure
351+
install(
352+
DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/src/"
353+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/src"
354+
FILES_MATCHING
355+
PATTERN "*.h" PATTERN "*.hpp"
356+
)
357+
358+
# Generate the *Config.cmake file for find_package()
359+
configure_package_config_file(
360+
"${ARM_COMPUTE_CONFIG_TEMPLATE_FILE}"
361+
"${ARM_COMPUTE_CONFIG_OUTPUT_FILE}"
362+
INSTALL_DESTINATION ${ARM_COMPUTE_CONFIG_INSTALL_DIR}
363+
)
364+
365+
# Configure *ConfigVersion.cmake which exports the version info
366+
write_basic_package_version_file(
367+
"${ARM_COMPUTE_CONFIG_VERSION_OUTPUT_FILE}"
368+
VERSION ${${PROJECT_NAME}_VERSION}
369+
COMPATIBILITY AnyNewerVersion
370+
)
371+
372+
# Install the generated *Config.cmake for find_package()
373+
install(
374+
FILES "${ARM_COMPUTE_CONFIG_OUTPUT_FILE}" "${ARM_COMPUTE_CONFIG_VERSION_OUTPUT_FILE}"
375+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
376+
)

cmake/ArmComputeConfig.cmake.in

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright (c) 2025 Arm Limited.
2+
#
3+
# SPDX-License-Identifier: MIT
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to
7+
# deal in the Software without restriction, including without limitation the
8+
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9+
# sell copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
23+
@PACKAGE_INIT@
24+
25+
include("${CMAKE_CURRENT_LIST_DIR}/@[email protected]")
26+
27+
# Manually re-create the alias for installed projects
28+
if(NOT TARGET @PROJECT_NAME@::Core)
29+
add_library(@PROJECT_NAME@::Core ALIAS @PROJECT_NAME@::arm_compute)
30+
endif()
31+
32+
# Manually re-create the alias for installed projects
33+
if(NOT TARGET @PROJECT_NAME@::Graph)
34+
add_library(@PROJECT_NAME@::Graph ALIAS @PROJECT_NAME@::arm_compute_graph)
35+
endif()

cmake/compilers/setup.cmake

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323
include(${CMAKE_CURRENT_LIST_DIR}/gcc.cmake)
2424
include(${CMAKE_CURRENT_LIST_DIR}/clang.cmake)
2525

26+
# Handle unique library artifacts when using a multi-config generator
27+
if(CMAKE_GENERATOR MATCHES "Multi-Config")
28+
message(STATUS "Using multi-config generator. Will append '_d' to debug libraries")
29+
set(CMAKE_DEBUG_POSTFIX "_d")
30+
else()
31+
message(STATUS "Using single-config generator.")
32+
endif()
33+
2634
# Common definitions for all projects.
2735
set(
2836
ARM_COMPUTE_DEFINES
@@ -126,9 +134,3 @@ string(PREPEND ARM_COMPUTE_ARCH -march=)
126134
string(PREPEND ARM_COMPUTE_CORE_FP16_ARCH -march=)
127135
string(PREPEND ARM_COMPUTE_SVE_ARCH -march=)
128136
string(PREPEND ARM_COMPUTE_SVE2_ARCH -march=)
129-
130-
# Remove any CMake additions so we have clean build line.
131-
set(CMAKE_CXX_FLAGS "" CACHE STRING "" FORCE)
132-
set(CMAKE_CXX_FLAGS_INIT "" CACHE STRING "" FORCE)
133-
set(CMAKE_CXX_FLAGS_DEBUG "" CACHE STRING "" FORCE)
134-
set(CMAKE_CXX_FLAGS_RELEASE "" CACHE STRING "" FORCE)

0 commit comments

Comments
 (0)