浏览代码

Adapt CMake to support Android

Android CMake is too old that the CMake script needs to be modified significantly.
Miku AuahDark 4 年之前
父节点
当前提交
ec8529c12b
共有 2 个文件被更改,包括 52 次插入47 次删除
  1. 51 47
      src/CMakeLists.txt
  2. 1 0
      src/common/config.h.in

+ 51 - 47
src/CMakeLists.txt

@@ -1,48 +1,15 @@
 cmake_minimum_required (VERSION 3.0)
 cmake_minimum_required (VERSION 3.0)
 
 
-### Basic compilation settings
-set (CMAKE_POSITION_INDEPENDENT_CODE TRUE)
-add_definitions (-DNOMINMAX)
-
-include_directories (
-	${CMAKE_CURRENT_SOURCE_DIR}
-	${CMAKE_CURRENT_BINARY_DIR}
-)
-
-### Compiler-specific flags
-if (MSVC)
-	set (DLLEXPORT "__declspec(dllexport)")
-elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
-	set (DLLEXPORT "__attribute__((visibility(\"default\")))")
-	add_compile_options ("-fvisibility=hidden")
-	add_link_options ("-fvisibility=hidden")
-endif ()
-
-### "Libraries"
-add_library (https MODULE
+set (HTTPS_SOURCES
 	lua/main.cpp
 	lua/main.cpp
-)
-
-add_library (https-common STATIC
 	common/HTTPRequest.cpp
 	common/HTTPRequest.cpp
 	common/HTTPSClient.cpp
 	common/HTTPSClient.cpp
 	common/PlaintextConnection.cpp
 	common/PlaintextConnection.cpp
 )
 )
-
-add_library (https-curl STATIC EXCLUDE_FROM_ALL
-	generic/CurlClient.cpp
-)
-
-add_library (https-openssl STATIC EXCLUDE_FROM_ALL
-	generic/OpenSSLConnection.cpp
-)
-
-add_library (https-schannel STATIC EXCLUDE_FROM_ALL
-	windows/SChannelConnection.cpp
-)
-
-add_library (https-nsurl STATIC EXCLUDE_FROM_ALL
-	macos/NSURLClient.mm
+set (HTTPS_LINK_LIBRARIES)
+set (HTTPS_INCLUDE_DIRECTORIES
+	${CMAKE_CURRENT_SOURCE_DIR}
+	${CMAKE_CURRENT_BINARY_DIR}
 )
 )
 
 
 ### Flags
 ### Flags
@@ -51,6 +18,7 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
 	option (USE_OPENSSL_BACKEND "Use the openssl backend" ON)
 	option (USE_OPENSSL_BACKEND "Use the openssl backend" ON)
 	option (USE_SCHANNEL_BACKEND "Use the schannel backend (windows-only)" OFF)
 	option (USE_SCHANNEL_BACKEND "Use the schannel backend (windows-only)" OFF)
 	option (USE_NSURL_BACKEND "Use the NSUrl backend (macos-only)" OFF)
 	option (USE_NSURL_BACKEND "Use the NSUrl backend (macos-only)" OFF)
+	option (USE_ANDROID_BACKEND "Use the Android Java backend (Android-only)" OFF)
 
 
 	option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" OFF)
 	option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" OFF)
 elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
 elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
@@ -58,6 +26,7 @@ elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
 	option (USE_OPENSSL_BACKEND "Use the openssl backend" OFF)
 	option (USE_OPENSSL_BACKEND "Use the openssl backend" OFF)
 	option (USE_SCHANNEL_BACKEND "Use the schannel backend (windows-only)" ON)
 	option (USE_SCHANNEL_BACKEND "Use the schannel backend (windows-only)" ON)
 	option (USE_NSURL_BACKEND "Use the NSUrl backend (macos-only)" OFF)
 	option (USE_NSURL_BACKEND "Use the NSUrl backend (macos-only)" OFF)
+	option (USE_ANDROID_BACKEND "Use the Android Java backend (Android-only)" OFF)
 
 
 	option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" ON)
 	option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" ON)
 elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
 elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
@@ -65,36 +34,71 @@ elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
 	option (USE_OPENSSL_BACKEND "Use the openssl backend" OFF)
 	option (USE_OPENSSL_BACKEND "Use the openssl backend" OFF)
 	option (USE_SCHANNEL_BACKEND "Use the schannel backend (windows-only)" OFF)
 	option (USE_SCHANNEL_BACKEND "Use the schannel backend (windows-only)" OFF)
 	option (USE_NSURL_BACKEND "Use the NSUrl backend (macos-only)" ON)
 	option (USE_NSURL_BACKEND "Use the NSUrl backend (macos-only)" ON)
+	option (USE_ANDROID_BACKEND "Use the Android Java backend (Android-only)" OFF)
+
+	option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" OFF)
+elseif (ANDROID)
+	option (USE_CURL_BACKEND "Use the libcurl backend" OFF)
+	option (USE_OPENSSL_BACKEND "Use the openssl backend" OFF)
+	option (USE_SCHANNEL_BACKEND "Use the schannel backend (windows-only)" OFF)
+	option (USE_NSURL_BACKEND "Use the NSUrl backend (macos-only)" OFF)
+	option (USE_ANDROID_BACKEND "Use the Android Java backend (Android-only)" ON)
 
 
 	option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" OFF)
 	option (USE_WINSOCK "Use winsock instead of BSD sockets (windows-only)" OFF)
 endif ()
 endif ()
 option (DEBUG_SCHANNEL "Enable debug output in schannel backend" OFF)
 option (DEBUG_SCHANNEL "Enable debug output in schannel backend" OFF)
 
 
 ### Dependencies
 ### Dependencies
-target_link_libraries (https https-common)
 
 
 find_package (Lua 5.1 REQUIRED)
 find_package (Lua 5.1 REQUIRED)
-include_directories (${LUA_INCLUDE_DIR})
-target_link_libraries (https ${LUA_LIBRARIES})
+list (APPEND HTTPS_INCLUDE_DIRECTORIES ${LUA_INCLUDE_DIR})
+list (APPEND HTTPS_LINK_LIBRARIES ${LUA_LIBRARIES})
 
 
 if (USE_CURL_BACKEND)
 if (USE_CURL_BACKEND)
 	find_package (CURL REQUIRED)
 	find_package (CURL REQUIRED)
-	include_directories (${CURL_INCLUDE_DIR})
-	target_link_libraries (https https-curl ${CURL_LIBRARIES})
+	list (APPEND HTTPS_SOURCES generic/CurlClient.cpp)
+	list (APPEND HTTPS_INCLUDE_DIRECTORIES ${CURL_INCLUDE_DIR})
+	list (APPEND HTTPS_LINK_LIBRARIES ${CURL_LIBRARIES})
 endif ()
 endif ()
 
 
 if (USE_OPENSSL_BACKEND)
 if (USE_OPENSSL_BACKEND)
 	find_package (OpenSSL REQUIRED)
 	find_package (OpenSSL REQUIRED)
-	include_directories (${OPENSSL_INCLUDE_DIR})
-	target_link_libraries (https https-openssl ${OPENSSL_LIBRARIES})
+	list (APPEND HTTPS_SOURCES generic/OpenSSLConnection.cpp)
+	list (APPEND HTTPS_INCLUDE_DIRECTORIES ${OPENSSL_INCLUDE_DIR})
+	list (APPEND HTTPS_LINK_LIBRARIES ${OPENSSL_LIBRARIES})
 endif ()
 endif ()
 
 
 if (USE_SCHANNEL_BACKEND)
 if (USE_SCHANNEL_BACKEND)
-	target_link_libraries (https https-schannel ws2_32 secur32)
+	list (APPEND HTTPS_SOURCES windows/SChannelConnection.cpp)
+	list (APPEND HTTPS_LINK_LIBRARIES ws2_32 secur32)
 endif ()
 endif ()
 
 
 if (USE_NSURL_BACKEND)
 if (USE_NSURL_BACKEND)
-	target_link_libraries (https https-nsurl)
+	list (APPEND HTTPS_SOURCES macos/NSURLClient.mm)
+endif ()
+
+if (USE_ANDROID_BACKEND)
+	list (APPEND HTTPS_SOURCES android/AndroidClient.cpp)
+endif ()
+
+### Main library
+add_library (https MODULE ${HTTPS_SOURCES})
+
+### Target options
+target_include_directories (https PRIVATE ${HTTPS_INCLUDE_DIRECTORIES})
+target_compile_definitions (https PRIVATE NOMINMAX)
+target_link_libraries (https ${HTTPS_LINK_LIBRARIES})
+set_target_properties (https PROPERTIES
+	POSITION_INDEPENDENT_CODE ON
+	CXX_VISIBILITY_PRESET hidden
+	PREFIX ""
+)
+
+### Compiler-specific flags
+if (MSVC)
+	set (DLLEXPORT "__declspec(dllexport)")
+elseif (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
+	set (DLLEXPORT "__attribute__((visibility(\"default\")))")
 endif ()
 endif ()
 
 
 ### Generate config.h
 ### Generate config.h

+ 1 - 0
src/common/config.h.in

@@ -2,6 +2,7 @@
 #cmakedefine USE_OPENSSL_BACKEND
 #cmakedefine USE_OPENSSL_BACKEND
 #cmakedefine USE_SCHANNEL_BACKEND
 #cmakedefine USE_SCHANNEL_BACKEND
 #cmakedefine USE_NSURL_BACKEND
 #cmakedefine USE_NSURL_BACKEND
+#cmakedefine USE_ANDROID_BACKEND
 #cmakedefine USE_WINSOCK
 #cmakedefine USE_WINSOCK
 #define DLLEXPORT @DLLEXPORT@
 #define DLLEXPORT @DLLEXPORT@
 #cmakedefine DEBUG_SCHANNEL
 #cmakedefine DEBUG_SCHANNEL