Skip to content

Commit

Permalink
CMake-based build with package and AAR building support.
Browse files Browse the repository at this point in the history
  • Loading branch information
rpavlik committed Jan 24, 2022
1 parent 79df644 commit 0d40049
Show file tree
Hide file tree
Showing 19 changed files with 532 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .cmake-format.py
@@ -0,0 +1,19 @@
# SPDX-FileCopyrightText: 2022, Collabora, Ltd.
# SPDX-License-Identifier: CC0-1.0

with section("format"):
line_width = 100
tab_size = 4

max_prefix_chars = 4

max_pargs_hwrap = 4
max_rows_cmdline = 1

keyword_case = 'upper'


# Do not reflow comments

with section("markup"):
enable_markup = False
10 changes: 10 additions & 0 deletions .gitignore
@@ -0,0 +1,10 @@
# SPDX-License-Identifier: CC0-1.0
# SPDX-FileCopyrightText: 2022, Collabora, Ltd.

build*/
install*/
.vscode/
CMakeUserPresets.json
maven_repo/*
/*.pom
/*.aar
91 changes: 91 additions & 0 deletions CMakeLists.txt
@@ -0,0 +1,91 @@
# Copyright 2021-2022, Collabora, Ltd.
#
# SPDX-License-Identifier: BSL-1.0

cmake_minimum_required(VERSION 3.10)
project(
percetto
VERSION 0.1.6.0
DESCRIPTION "A C wrapper for the C++ Perfetto tracing library.")

if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
# we are the top level project
option(BUILD_EXAMPLES "Should we build the examples?" ON)
else()
set(BUILD_EXAMPLES OFF)
endif()

if(NOT PERFETTO_SDK_PATH)
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/../perfetto/sdk)
set(PERFETTO_SDK_PATH
${CMAKE_CURRENT_SOURCE_DIR}/../perfetto/sdk
CACHE PATH "The SDK directory of a recent Perfetto release.")
else()
message(FATAL_ERROR "Must set PERFETTO_SDK_PATH")
endif()
endif()

# Basic config stuff
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_CXX_STANDARD 17)
include(GNUInstallDirs)
find_package(Threads REQUIRED)

if(ANDROID)
find_library(ANDROID_LOG log)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
option(INSTALL_FOR_PREFAB "Should we install for a manually-assembled prefab layout?" OFF)
if(INSTALL_FOR_PREFAB)
include(PrefabHelper)
setup_prefab()

setup_prefab_module(percetto)
endif()
endif()

# Perfetto SDK
add_library(perfetto OBJECT ${PERFETTO_SDK_PATH}/perfetto.cc ${PERFETTO_SDK_PATH}/perfetto.h)
target_link_libraries(perfetto PUBLIC Threads::Threads)
target_include_directories(perfetto PUBLIC $<BUILD_INTERFACE:${PERFETTO_SDK_PATH}>)
target_compile_options(perfetto PRIVATE -ffunction-sections -fdata-sections)

# PerCetto
add_subdirectory(src)

# Optional examples
if(BUILD_EXAMPLES)
add_subdirectory(examples)
endif()

if("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
# Version usable in both build and install dirs.
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/percetto/percettoConfigVersion.cmake"
VERSION ${percetto_VERSION}
COMPATIBILITY AnyNewerVersion)
# This really minimal config file usable in both build and install dirs
configure_file(cmake/percettoConfig.cmake
"${CMAKE_CURRENT_BINARY_DIR}/percetto/percettoConfig.cmake" COPYONLY)

# Exported targets usable only for build
export(
EXPORT percetto
FILE "${CMAKE_CURRENT_BINARY_DIR}/percetto/percettoTargets.cmake"
NAMESPACE percetto::)

# Installed targets usable only for installed
set(PACKAGE_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/percetto)
install(
EXPORT percetto
FILE percettoTargets.cmake
NAMESPACE percetto
DESTINATION ${PACKAGE_INSTALL_DIR})

# Install the files we share with the build tree.
install(
FILES cmake/percettoConfig.cmake
"${CMAKE_CURRENT_BINARY_DIR}/percetto/percettoConfigVersion.cmake"
DESTINATION ${PACKAGE_INSTALL_DIR})
endif()
135 changes: 135 additions & 0 deletions CMakePresets.json
@@ -0,0 +1,135 @@
{
"version": 3,
"cmakeMinimumRequired": {
"major": 3,
"minor": 21,
"patch": 0
},
"configurePresets": [
{
"name": "Android_base",
"displayName": "Base build for Android",
"generator": "Ninja",
"hidden": true,
"cacheVariables": {
"ANDROID_PLATFORM": "26",
"ANDROID_STL": "c++_shared",
"CMAKE_ANDROID_NDK": "$env{ANDROID_NDK_HOME}",
"CMAKE_BUILD_TYPE": "RelWithDebInfo"
},
"toolchainFile": "$env{ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake",
"binaryDir": "${sourceDir}/build/${presetName}",
"installDir": "${sourceDir}/install/${presetName}"
},
{
"name": "Android_x86",
"inherits": "Android_base",
"cacheVariables": {
"ANDROID_ABI": "x86"
}
},
{
"name": "Android_x86_64",
"inherits": "Android_base",
"cacheVariables": {
"ANDROID_ABI": "x86_64"
}
},
{
"name": "Android_armeabi-v7a",
"inherits": "Android_base",
"cacheVariables": {
"ANDROID_ABI": "armeabi-v7a"
}
},
{
"name": "Android_arm64-v8a",
"inherits": "Android_base",
"cacheVariables": {
"ANDROID_ABI": "arm64-v8a"
}
},
{
"name": "Android_aar_base",
"inherits": "Android_base",
"cacheVariables": {
"INSTALL_FOR_PREFAB": "ON"
},
"installDir": "install/percetto",
"hidden": true
},
{
"name": "Android_x86_64_aar",
"inherits": "Android_aar_base",
"cacheVariables": {
"ANDROID_ABI": "x86_64"
}
},
{
"name": "Android_armeabi-v7a_aar",
"inherits": "Android_aar_base",
"cacheVariables": {
"ANDROID_ABI": "armeabi-v7a"
}
},
{
"name": "Android_arm64-v8a_aar",
"inherits": "Android_aar_base",
"cacheVariables": {
"ANDROID_ABI": "arm64-v8a"
}
},
{
"name": "Android_x86_aar",
"inherits": "Android_aar_base",
"cacheVariables": {
"ANDROID_ABI": "x86"
}
},
{
"name": "Linux_Ninja",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build",
"installDir": "${sourceDir}/install"
},
{
"name": "Linux_Ninja_Clang",
"inherits": "Linux_Ninja",
"cacheVariables": {
"CMAKE_C_COMPILER": "/usr/bin/clang",
"CMAKE_CXX_COMPILER": "/usr/bin/clang++"
}
}
],
"buildPresets": [
{
"name": "Android_aar_base",
"hidden": true,
"targets": [
"all",
"install"
],
"verbose": true
},
{
"name": "Android_x86_aar",
"inherits": "Android_aar_base",
"configurePreset": "Android_x86_aar"
},
{
"name": "Android_x86_64_aar",
"inherits": "Android_aar_base",
"configurePreset": "Android_x86_64_aar"
},
{
"name": "Android_armeabi-v7a_aar",
"inherits": "Android_aar_base",
"configurePreset": "Android_armeabi-v7a_aar"
},
{
"name": "Android_arm64-v8a_aar",
"inherits": "Android_aar_base",
"configurePreset": "Android_arm64-v8a_aar"
}
]
}
2 changes: 2 additions & 0 deletions CMakePresets.json.license
@@ -0,0 +1,2 @@
SPDX-FileCopyrightText: 2022, Collabora, Ltd.
SPDX-License-Identifier: CC0-1.0
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -25,6 +25,15 @@ meson build -Dperfetto-sdk=path/to/perfetto/sdk
meson compile -C build
```

Alternately, you can use CMake. It will assume that you've cloned a recent
release of Perfetto next to the percetto directory, unless you pass
`-DPERFETTO_SDK_PATH=some/other/location`.

```sh
cmake -S . -B build -G Ninja
ninja -C build
```

## Directory Structure

`perfetto-sdk` contains rules to locate and build Perfetto SDK.
Expand Down
53 changes: 53 additions & 0 deletions build-aar.sh
@@ -0,0 +1,53 @@
#!/usr/bin/env bash
# Copyright 2021-2022, Collabora, Ltd.
#
# SPDX-License-Identifier: BSL-1.0

set -e

# shellcheck disable=SC2086
ROOT=$(cd "$(dirname $0)" && pwd)
PROJECT=percetto
DEFAULT_NDK_VERSION=21.4.7075529

ANDROID_NDK_HOME=${ANDROID_NDK_HOME:-${HOME}/Android/Sdk/ndk/${DEFAULT_NDK_VERSION}}

if [ ! -f "${ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake" ]; then
echo "Please set ANDROID_NDK_HOME to a valid, installed NDK!"
exit 1
fi

export ANDROID_NDK_HOME

BUILD_DIR=${BUILD_DIR:-${ROOT}/build}
INSTALL_DIR=${INSTALL_DIR:-${ROOT}/install}
GENERATION_DIR="${BUILD_DIR}/Android_x86_aar/src"

rm -f "${GENERATION_DIR}/*.pom" || true

rm -rf "${INSTALL_DIR}"
for arch in x86 x86_64 armeabi-v7a arm64-v8a; do
cmake --preset Android_${arch}_aar
cmake --build --preset Android_${arch}_aar
done
# find latest pom
DECORATED=$(cd "${GENERATION_DIR}" && find . -maxdepth 1 -name "${PROJECT}*.pom" | sort | tail -n 1)
# strip leading ./
DECORATED=${DECORATED#./}
echo "DECORATED ${DECORATED}"
DECORATED_STEM=${DECORATED%.pom}
echo "DECORATED_STEM ${DECORATED_STEM}"
VERSION=${DECORATED_STEM#${PROJECT}-}
echo "VERSION ${VERSION}"
DIR=$(pwd)/maven_repo/io/github/olvaffe/deps/${PROJECT}/${VERSION}

mkdir -p "${DIR}"
cp "${GENERATION_DIR}/${DECORATED}" "${ROOT}"
cp "${GENERATION_DIR}/${DECORATED}" "${DIR}"

(
cd "$INSTALL_DIR/${PROJECT}"
7za a -r ../${PROJECT}.zip ./*
mv ../${PROJECT}.zip "$ROOT/${DECORATED_STEM}.aar"
cp "$ROOT/${DECORATED_STEM}.aar" "$DIR/${DECORATED_STEM}.aar"
)
60 changes: 60 additions & 0 deletions cmake/PrefabHelper.cmake
@@ -0,0 +1,60 @@
# Copyright 2020-2021, Collabora, Ltd.
#
# SPDX-License-Identifier: BSL-1.0
#
# Helpers for creating an android "prefab" archive

# We must run the following at "include" time, not at function call time,
# to find the path to this module rather than the path to a calling list file
get_filename_component(_prefab_mod_dir ${CMAKE_CURRENT_LIST_FILE} PATH)

macro(setup_prefab)
set(PREFAB_INSTALL_DIR prefab)
unset(NDK_MAJOR_VERSION)
if(CMAKE_ANDROID_NDK)
file(STRINGS "${CMAKE_ANDROID_NDK}/source.properties" NDK_PROPERTIES)
foreach(_line ${NDK_PROPERTIES})
if("${_line}" MATCHES
"Pkg.Revision = ([0-9]+)[.]([0-9]+)[.]([0-9]+)")
set(NDK_MAJOR_VERSION ${CMAKE_MATCH_1})
endif()
endforeach()
else()
message(FATAL_ERROR "Please set CMAKE_ANDROID_NDK to your NDK root!")
endif()
if(NDK_MAJOR_VERSION)
message(STATUS "Building using NDK major version ${NDK_MAJOR_VERSION}")
else()
message(
FATAL_ERROR
"Could not parse the major version from ${CMAKE_ANDROID_NDK}/source.properties"
)
endif()

configure_file(${_prefab_mod_dir}/prefab.json
${CMAKE_CURRENT_BINARY_DIR}/prefab.json @ONLY)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/prefab.json
DESTINATION ${PREFAB_INSTALL_DIR}
COMPONENT Prefab)
endmacro()

macro(setup_prefab_module MODULE_NAME)
set(PREFAB_MODULE_INSTALL_DIR ${PREFAB_INSTALL_DIR}/modules/${MODULE_NAME})
set(CMAKE_INSTALL_LIBDIR
${PREFAB_MODULE_INSTALL_DIR}/libs/android.${ANDROID_ABI})
set(CMAKE_INSTALL_BINDIR ${CMAKE_INSTALL_LIBDIR})
set(CMAKE_INSTALL_INCLUDEDIR ${PREFAB_MODULE_INSTALL_DIR}/${CMAKE_INSTALL_INCLUDEDIR})

configure_file(${_prefab_mod_dir}/abi.json
${CMAKE_CURRENT_BINARY_DIR}/abi.json @ONLY)
install(
FILES ${CMAKE_CURRENT_BINARY_DIR}/abi.json
DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT Prefab)

install(
FILES ${_prefab_mod_dir}/module.json
DESTINATION ${PREFAB_MODULE_INSTALL_DIR}
COMPONENT Prefab)
endmacro()
6 changes: 6 additions & 0 deletions cmake/abi.json
@@ -0,0 +1,6 @@
{
"abi": "@ANDROID_ABI@",
"api": @CMAKE_SYSTEM_VERSION@,
"ndk": @NDK_MAJOR_VERSION@,
"stl": "@ANDROID_STL@"
}

0 comments on commit 0d40049

Please sign in to comment.