Skip to content

Commit 1e4c599

Browse files
Implement GenerateConanToolchainFileTask
1 parent 248d085 commit 1e4c599

3 files changed

Lines changed: 86 additions & 5 deletions

File tree

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
## Why is this needed?
44

5-
In order to use Conan dependencies in an Android project, `conan install` needs to be executed before CMake tries to find those libraries. This plugin calls `conan install` for all 4 Android's architectures.
5+
In order to use Conan dependencies in an Android project, `conan install` needs to be executed before CMake tries to find those libraries.
66

77
## How it works
88

9-
Conan Android Gradle Plugin creates 4 Gradle tasks: `conanInstall-armv7`, `conanInstall-armv8`, `conanInstall-x86`, `conanInstall-x86_64` - one for each architecture, for which `conan install` will be launched.
9+
Conan Android Gradle Plugin creates 5 Gradle tasks:
1010

11-
`conanInstall` tasks are added as dependencies to module level `preBuild` task and top level `prepareKotlinBuildScriptModel` task, if it's found.
11+
- Four `conan install` tasks, to install conan dependencies for each architecture: `conanInstall-armv7`, `conanInstall-armv8`, `conanInstall-x86`, `conanInstall-x86_64`.
1212

13-
`preBuild` task is executed before building Android project. `prepareKotlinBuildScriptModel` task is executed when doing Android Studio sync.
13+
- One additional `GenerateConanToolchainFileTask` task, to generate `build/conan/android_toolchain.cmake`.
1414

15+
Tasks are added as dependencies to module level `preBuild` task and top level `prepareKotlinBuildScriptModel` task, if it is found.
16+
17+
`preBuild` task is executed before building Android project. `prepareKotlinBuildScriptModel` task is Android Studio sync task.
1518

1619
## How to use
1720

@@ -41,10 +44,22 @@ Once the plugin is applied, it needs to be configured. Configuration currently c
4144
}
4245
```
4346

44-
Conan profile can be either a relative path or an absolute path, it could also be a regular conan profile, installed and managed by conan (eg. `default`).
47+
Conan profile can be either a relative path or an absolute path, it could also be a regular conan profile, installed and managed by conan (e.g. `default`).
4548

4649
Plugin applies `arch` setting when calling `conan install`, thus all architectures can share the same profile, if no differentiation is needed - `{ profile.set("android") }`.
4750

51+
Plugin task generate CMake toolchain file, which needs to be included by CMake:
52+
53+
```groovy
54+
android {
55+
defaultConfig {
56+
externalNativeBuild.cmake.arguments(
57+
"-DCMAKE_TOOLCHAIN_FILE=build/conan/android_toolchain.cmake"
58+
)
59+
}
60+
}
61+
```
62+
4863
## Further reading
4964

5065
https://docs.conan.io/2/examples/cross_build/android/android_studio.html

src/main/kotlin/app/opendocument/ConanAndroidGradlePlugin.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class ConanAndroidGradlePlugin: Plugin<Project> {
2929
val tasks = target.tasks;
3030
val preBuild = tasks.named("preBuild").get()
3131
val syncTask = target.parent?.tasks?.named("prepareKotlinBuildScriptModel")?.get()
32+
33+
val generateToolchainFileTask = tasks.register("generateToolchainFile", GenerateConanToolchainFileTask::class.java)
34+
preBuild.dependsOn(generateToolchainFileTask)
35+
syncTask?.dependsOn(generateToolchainFileTask)
36+
3237
listOf("armv8", "armv7", "x86", "x86_64").forEach { architecture ->
3338
val conanInstallTask = tasks.register("conanInstall-$architecture", ConanInstallTask::class.java) { conanInstallTask ->
3439
conanInstallTask.arch.set(architecture)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* GenerateConanToolchainFileTask.kt
3+
*
4+
* ConanAndroidGradlePlugin (https://github.com/opendocument-app/ConanAndroidGradlePlugin)
5+
*
6+
* Copyright (c) 2024 ViliusSutkus89.com OpenDocument.app
7+
*
8+
* ConanAndroidGradlePlugin is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License version 3 as
10+
* published by the Free Software Foundation.
11+
*
12+
* ConanAndroidGradlePlugin is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
package app.opendocument
22+
23+
import org.gradle.api.DefaultTask
24+
import org.gradle.api.file.RegularFileProperty
25+
import org.gradle.api.tasks.OutputFile
26+
import org.gradle.api.tasks.TaskAction
27+
28+
29+
abstract class GenerateConanToolchainFileTask : DefaultTask() {
30+
@get:OutputFile
31+
abstract val conanToolchainFile: RegularFileProperty
32+
init {
33+
conanToolchainFile.convention(project.layout.buildDirectory.get().dir("conan").file("android_toolchain.cmake"))
34+
}
35+
36+
@TaskAction
37+
fun writeToolchain() {
38+
val ANDROID_ABI = "\${ANDROID_ABI}"
39+
val CMAKE_CURRENT_LIST_DIR = "\${CMAKE_CURRENT_LIST_DIR}"
40+
val CMAKE_BUILD_TYPE = "\${CMAKE_BUILD_TYPE}"
41+
conanToolchainFile.get().asFile.writeText("""
42+
# During multiple stages of CMake configuration, the toolchain file is processed and command-line
43+
# variables may not be always available. The script exits prematurely if essential variables are absent.
44+
45+
if ( NOT ANDROID_ABI OR NOT CMAKE_BUILD_TYPE )
46+
return()
47+
endif()
48+
if(${ANDROID_ABI} STREQUAL "x86_64")
49+
include("${CMAKE_CURRENT_LIST_DIR}/x86_64/build/${CMAKE_BUILD_TYPE}/generators/conan_toolchain.cmake")
50+
elseif(${ANDROID_ABI} STREQUAL "x86")
51+
include("${CMAKE_CURRENT_LIST_DIR}/x86/build/${CMAKE_BUILD_TYPE}/generators/conan_toolchain.cmake")
52+
elseif(${ANDROID_ABI} STREQUAL "arm64-v8a")
53+
include("${CMAKE_CURRENT_LIST_DIR}/armv8/build/${CMAKE_BUILD_TYPE}/generators/conan_toolchain.cmake")
54+
elseif(${ANDROID_ABI} STREQUAL "armeabi-v7a")
55+
include("${CMAKE_CURRENT_LIST_DIR}/armv7/build/${CMAKE_BUILD_TYPE}/generators/conan_toolchain.cmake")
56+
else()
57+
message(FATAL "Not supported configuration")
58+
endif()
59+
""".trimIndent())
60+
}
61+
}

0 commit comments

Comments
 (0)