summaryrefslogtreecommitdiff
path: root/vendor/curl.cmake
blob: 97ee9d4e733a69f5c601a49da3d2e42180c0da30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
if(TARGET mbgl-vendor-curl)
    return()
endif()

# Use prebuilt libraries by dedault
if(NOT BUILD_ANDROID_CURL)
    add_library(
        mbgl-vendor-curl INTERFACE
    )

    set(CURL_BIN_DIR ${CMAKE_CURRENT_LIST_DIR}/mapbox-gl-native-curl/prebuilt/android)
    set(CURL_STATIC_LIB ${CURL_BIN_DIR}/${ANDROID_ABI}/libcurl.a)
    set(SSL_STATIC_LIB ${CURL_BIN_DIR}/${ANDROID_ABI}/libssl.a)
    set(CRYPTO_STATIC_LIB ${CURL_BIN_DIR}/${ANDROID_ABI}/libcrypto.a)

    target_link_libraries(
        mbgl-vendor-curl
        INTERFACE
            $<$<PLATFORM_ID:Android>:${CURL_STATIC_LIB}> $<$<PLATFORM_ID:Android>:${SSL_STATIC_LIB}>
            $<$<PLATFORM_ID:Android>:${CRYPTO_STATIC_LIB}>
    )

    target_include_directories(
        mbgl-vendor-curl SYSTEM
        INTERFACE ${CMAKE_CURRENT_LIST_DIR}/mapbox-gl-native-curl/include
    )
    return()
endif()

# Build Curl with OpenSSL support

include(ExternalProject)

include(ProcessorCount)
processorcount(NPROC)

# Check if Android NDK path is defined in the environment
if(DEFINED ENV{ANDROID_HOME})
    set(ANDROID_NDK_HOME $ENV{ANDROID_HOME}/ndk-bundle)
elseif(DEFINED ENV{ANDROID_SDK_ROOT})
    set(ANDROID_NDK_HOME $ENV{ANDROID_SDK_ROOT}/ndk-bundle)
elseif(DEFINED ANDROID_HOME)
    set(ANDROID_NDK_HOME ${ANDROID_HOME}/ndk-bundle)
elseif(DEFINED ANDROID_SDK_ROOT)
    set(ANDROID_NDK_HOME ${ANDROID_SDK_ROOT}/ndk-bundle)
else()
    message(FATAL_ERROR "Must set either ANDROID_HOME or ANDROID_SDK_ROOT variable")
endif()

if(CMAKE_HOST_SYSTEM_NAME STREQUAL Linux)
    set(HOST_TAG linux-x86_64)
elseif(CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin)
    set(HOST_TAG darwin-x86_64)
else()
    message(FATAL_ERROR "Unsupported host: " ${CMAKE_SYSTEM_NAME})
endif()

if(${ANDROID_ABI} STREQUAL arm64-v8a)
    set(TRIPLE aarch64-linux-android)
    set(SSL_TARGET_ARCH android-arm64)
elseif(${ANDROID_ABI} STREQUAL x86_64)
    set(TRIPLE x86_64-linux-android)
    set(SSL_TARGET_ARCH android-x86_64)
else()
    message(FATAL_ERROR "-- Unsupported Android ABI: " ${ANDROID_ABI})
endif()

set(TOOLCHAIN ${ANDROID_NDK_HOME}/toolchains/llvm/prebuilt/${HOST_TAG})
set(PATH ${TOOLCHAIN}/bin:$ENV{PATH})

set(CC ${TOOLCHAIN}/bin/${TRIPLE}${ANDROID_NATIVE_API_LEVEL}-clang)
set(CXX ${TOOLCHAIN}/bin/${TRIPLE}${ANDROID_NATIVE_API_LEVEL}-clang++)
set(AR ${TOOLCHAIN}/bin/${TRIPLE}-ar)
set(RANLIB ${TOOLCHAIN}/bin/${TRIPLE}-ranlib)

# Build OpenSSL
set(SSL_SRC_DIR ${CMAKE_CURRENT_LIST_DIR}/mapbox-gl-native-curl/openssl)
set(SSL_BIN_DIR ${CMAKE_CURRENT_BINARY_DIR})
set(SSL_INCLUDES ${SSL_BIN_DIR}/include)
file(MAKE_DIRECTORY ${SSL_INCLUDES})

set(SSL_STATIC_LIB ${SSL_BIN_DIR}/lib/libssl.a)
set(CRYPTO_STATIC_LIB ${SSL_BIN_DIR}/lib/libcrypto.a)

ExternalProject_Add(
    libssl
    PREFIX ${SSL_BIN_DIR}
    SOURCE_DIR ${SSL_SRC_DIR}
    DOWNLOAD_COMMAND
    GIT_REPOSITORY https://github.com/openssl/openssl.git
    GIT_TAG OpenSSL_1_1_1d
    GIT_SHALLOW true
    GIT_PROGRESS true
    CONFIGURE_COMMAND
        echo
        "Configuring openssl $<CONFIG>-${ANDROID_ABI}"
        &&
        ${CMAKE_COMMAND}
        -E
        env
        ANDROID_NDK_HOME=${ANDROID_NDK_HOME}
        PATH=${PATH}
        ${SSL_SRC_DIR}/Configure
        ${SSL_TARGET_ARCH}
        no-asm
        no-shared
        no-tests
        no-unit-test
        zlib
        -static
        --prefix=${SSL_BIN_DIR}
    BUILD_COMMAND
        echo
        "Building openssl $<CONFIG>-${ANDROID_ABI}"
        &&
        ${CMAKE_COMMAND}
        -E
        env
        ANDROID_NDK_HOME=${ANDROID_NDK_HOME}
        PATH=${PATH}
        make
        -j${NPROC}
    INSTALL_COMMAND
        echo
        "Installing openssl $<CONFIG>-${ANDROID_ABI}"
        &&
        make
        install
    BUILD_BYPRODUCTS ${SSL_STATIC_LIB} ${CRYPTO_STATIC_LIB}
)

add_library(
    android-ssl STATIC IMPORTED
)
add_dependencies(android-ssl libssl)
set_target_properties(android-ssl PROPERTIES IMPORTED_LOCATION ${SSL_STATIC_LIB})
set_target_properties(android-ssl PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${SSL_INCLUDES})

add_library(
    android-crypto STATIC IMPORTED
)
add_dependencies(android-crypto libssl)
set_target_properties(android-crypto PROPERTIES IMPORTED_LOCATION ${CRYPTO_STATIC_LIB})
set_target_properties(android-crypto PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${SSL_INCLUDES})

# Build CURL
set(CURL_DIR ${CMAKE_CURRENT_LIST_DIR}/mapbox-gl-native-curl/curl)
set(CURL_BIN_DIR ${CMAKE_CURRENT_BINARY_DIR})
set(CURL_INCLUDES ${CURL_BIN_DIR}/include)
file(MAKE_DIRECTORY ${CURL_INCLUDES})
set(CURL_STATIC_LIB ${CURL_BIN_DIR}/lib/libcurl.a)

ExternalProject_Add(
    libcurl
    PREFIX ${CURL_BIN_DIR}
    DEPENDS libssl
    SOURCE_DIR ${CURL_DIR}
    DOWNLOAD_COMMAND
    GIT_REPOSITORY https://github.com/curl/curl.git
    GIT_TAG curl-7_69_0
    GIT_SHALLOW true
    GIT_PROGRESS true
    UPDATE_COMMAND
        cd
        ${CURL_DIR}
        &&
        echo
        "Starting curl $<CONFIG>-${ANDROID_ABI} build"
        &&
        ${CURL_DIR}/buildconf
    CONFIGURE_COMMAND
        echo
        "Configuring curl $<CONFIG>-${ANDROID_ABI}"
        &&
        CC=${CC}
        CXX=${CXX}
        AR=${AR}
        RANLIB=${RANLIB}
        ${CURL_DIR}/configure
        --host=${TRIPLE}
        --target=${TRIPLE}
        --with-sysroot=${TOOLCHAIN}/sysroot
        --with-ssl=${SSL_BIN_DIR}
        --enable-static
        --disable-shared
        --disable-verbose
        --enable-threaded-resolver
        --enable-libgcc
        --enable-ipv6
        --disable-manual
        --with-pic
        --prefix=${CURL_BIN_DIR}
    BUILD_COMMAND
        echo
        "Building curl $<CONFIG>-${ANDROID_ABI}"
        &&
        make
        -j${NPROC}
    INSTALL_COMMAND
        echo
        "Installing curl $<CONFIG>-${ANDROID_ABI}"
        &&
        make
        install
    BUILD_BYPRODUCTS ${CURL_STATIC_LIB}
)

add_library(
    mbgl-vendor-curl STATIC IMPORTED
)
add_dependencies(libcurl libssl)
add_dependencies(
    mbgl-vendor-curl
    libcurl
    android-ssl
    android-crypto
)
set_target_properties(mbgl-vendor-curl PROPERTIES IMPORTED_LOCATION ${CURL_STATIC_LIB})
set_target_properties(mbgl-vendor-curl PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${CURL_INCLUDES})
set_target_properties(mbgl-vendor-curl PROPERTIES INTERFACE_COMPILE_DEFINITIONS CURL_STATICLIB)
set_target_properties(mbgl-vendor-curl PROPERTIES INTERFACE_LINK_LIBRARIES "-l${SSL_STATIC_LIB} -l${CRYPTO_STATIC_LIB}")