xref: /aosp_15_r20/external/sandboxed-api/contrib/libidn2/CMakeLists.txt (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1# Copyright 2022 Google LLC
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15cmake_minimum_required(VERSION 3.13..3.26)
16
17project(libidn2-sapi CXX C)
18include(CTest)
19include(GoogleTest)
20
21set(CMAKE_CXX_STANDARD 17)
22set(CMAKE_CXX_STANDARD_REQUIRED True)
23
24if(NOT TARGET sapi::sapi)
25  set(SAPI_ROOT "../.." CACHE PATH "Path to the Sandboxed API source tree")
26  add_subdirectory("${SAPI_ROOT}"
27                   "${CMAKE_BINARY_DIR}/sandboxed-api-build"
28                   # Omit this to have the full Sandboxed API in IDE
29                   EXCLUDE_FROM_ALL)
30endif()
31
32# Use pkg-config for the libidn2 include paths
33find_package(PkgConfig REQUIRED)
34pkg_check_modules(libidn2 REQUIRED IMPORTED_TARGET libidn2)
35
36# System libidn2 needs to have the libidn2-dev and libunistring-dev packages
37# installed. We cannot use pkg-config here, as libidn2's pkg-config file does
38# not support static linking well.
39find_library(libidn2 NAMES libidn2.a idn2)
40find_library(libunistring NAMES libunistring.a unistring)
41
42# Combine libidn2 and its dependency into a single static library
43add_library(idn2_static STATIC
44  "${SAPI_BINARY_DIR}/sapi_force_cxx_linkage.cc"
45)
46target_link_libraries(idn2_static PUBLIC
47  "-Wl,--whole-archive,${libidn2},--no-whole-archive"
48  "-Wl,--whole-archive,${libunistring},--no-whole-archive"
49)
50
51add_sapi_library(libidn2_sapi
52  FUNCTIONS idn2_lookup_u8 idn2_register_u8
53            idn2_strerror idn2_strerror_name
54            idn2_free idn2_to_ascii_8z
55            idn2_to_unicode_8z8z
56  INPUTS "${libidn2_INCLUDEDIR}/idn2.h"
57  LIBRARY idn2_static
58  LIBRARY_NAME IDN2
59  NAMESPACE ""
60)
61target_include_directories(libidn2_sapi INTERFACE
62  "${PROJECT_BINARY_DIR}"
63)
64
65add_library(libidn2_sapi_wrapper
66  libidn2_sapi.cc
67  libidn2_sapi.h
68)
69add_library(sapi_contrib::libidn2 ALIAS libidn2_sapi_wrapper)
70target_link_libraries(libidn2_sapi_wrapper
71  # PUBLIC so that the include directories are included in the interface
72  PUBLIC libidn2_sapi
73         sapi::base
74  PRIVATE absl::die_if_null
75          idn2
76)
77
78if(BUILD_TESTING AND SAPI_BUILD_TESTING)
79  add_subdirectory(tests)
80endif()
81