blob: c6229dcf0cd40b44a9fe707c0a5a701ff92cd898 [file] [log] [blame]
Yigit Boyarb87ae802023-10-25 08:33:25 -07001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Tiem Songee0da742024-01-03 14:08:46 -080017/**
18 * This file was created using the `create_project.py` script located in the
19 * `<AndroidX root>/development/project-creator` directory.
20 *
21 * Please use that script when creating a new project, rather than copying an existing project and
22 * modifying its settings.
23 */
Yigit Boyarb87ae802023-10-25 08:33:25 -070024import androidx.build.AndroidXConfig
Daniel Santiago Rivera6e1e6e12024-12-16 22:13:20 -050025import androidx.build.KotlinTarget
Yigit Boyarb87ae802023-10-25 08:33:25 -070026import androidx.build.PlatformIdentifier
Yigit Boyar20f1ff42024-01-30 16:23:52 +000027import androidx.build.ProjectLayoutType
Omar Ismail86e66062024-05-03 16:10:50 +010028import androidx.build.LibraryType
Yigit Boyarb87ae802023-10-25 08:33:25 -070029import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
30import org.jetbrains.kotlin.konan.target.Family
31import org.jetbrains.kotlin.konan.target.KonanTarget
32
33plugins {
34 id("AndroidXPlugin")
35 id("com.android.library")
36}
37
Yigit Boyar20f1ff42024-01-30 16:23:52 +000038apply(from: "PrepareSqliteSourcesTask.groovy")
39
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -050040// List of flags we use to compile SQLite.
41// See: https://www.sqlite.org/compile.html
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -050042def SQLITE_COMPILE_FLAGS = [
43 "-DHAVE_USLEEP=1",
Daniel Santiago Rivera7324def2024-06-07 12:32:08 -040044 "-DSQLITE_DEFAULT_AUTOVACUUM=1",
Daniel Santiago Rivera2617aa12023-12-22 11:56:10 -050045 "-DSQLITE_DEFAULT_MEMSTATUS=0",
Daniel Santiago Rivera7324def2024-06-07 12:32:08 -040046 "-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1",
47 "-DSQLITE_ENABLE_COLUMN_METADATA",
48 "-DSQLITE_ENABLE_FTS3",
49 "-DSQLITE_ENABLE_FTS3_PARENTHESIS",
50 "-DSQLITE_ENABLE_FTS4",
51 "-DSQLITE_ENABLE_FTS5",
52 "-DSQLITE_ENABLE_JSON1",
53 "-DSQLITE_ENABLE_NORMALIZE",
54 "-DSQLITE_ENABLE_RTREE",
55 "-DSQLITE_ENABLE_STAT4",
56 "-DSQLITE_HAVE_ISNAN",
57 "-DSQLITE_OMIT_BUILTIN_TEST",
58 "-DSQLITE_OMIT_DEPRECATED",
59 "-DSQLITE_OMIT_PROGRESS_CALLBACK",
60 "-DSQLITE_OMIT_SHARED_CACHE",
61 "-DSQLITE_SECURE_DELETE",
62 "-DSQLITE_TEMP_STORE=3",
Daniel Santiago Rivera2617aa12023-12-22 11:56:10 -050063 "-DSQLITE_THREADSAFE=2",
Yigit Boyarb87ae802023-10-25 08:33:25 -070064]
65
Yigit Boyar20f1ff42024-01-30 16:23:52 +000066// see PrepareSqliteSourcesTask.groovy for implementation details of the register method
67// This task prepares a directory to build sqlite and bundle it with the artifact.
68// SQLite is in public domain: https://www.sqlite.org/copyright.html.
69// Including it inside the library implies relicencing it to Apache 2.0.
70def prepareSqliteSourcesTask = registerPrepareSqliteSourcesTask(
71 project, // project
72 "prepareSqliteSourcesTask" // task name
73) {
Daniel Santiago Rivera3fdad752024-03-14 11:31:48 -040074 it.sqliteVersion.set("3.46.0")
75 it.sqliteReleaseYear.set(2024)
Yigit Boyar20f1ff42024-01-30 16:23:52 +000076 it.destinationDirectory.set(project.layout.buildDirectory.dir("sqlite3/src"))
77 if (!ProjectLayoutType.isPlayground(project)) {
78 it.sqlitePrebuiltsDirectory.set(
Daniel Santiago Rivera462028b2024-03-14 11:30:31 -040079 new File(AndroidXConfig.getExternalProjectPath(project), "sqlite/src")
Yigit Boyar20f1ff42024-01-30 16:23:52 +000080 )
Yigit Boyarb87ae802023-10-25 08:33:25 -070081 }
Yigit Boyarb87ae802023-10-25 08:33:25 -070082}
83
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -050084configurations {
Yigit Boyar20f1ff42024-01-30 16:23:52 +000085 // Configuration for producing a shareable archive file of compiled SQLite. Only the Linux X64
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -050086 // target of SQLite is produced hence the explicit name and attributes
87 linuxSharedArchive {
88 canBeConsumed = true
89 canBeResolved = false
90 attributes {
91 attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.NATIVE_LINK))
92 }
93 }
Yigit Boyar20f1ff42024-01-30 16:23:52 +000094 // Configuration for producing a shareable directory that includes SQLite amalgamation sources.
95 sqliteSources {
96 canBeConsumed = true
97 canBeResolved = false
98 attributes {
99 attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, "sqlite-amalgamation"))
100 }
101 }
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500102}
103
Yigit Boyarb87ae802023-10-25 08:33:25 -0700104androidXMultiplatform {
105 // List of targets for native compilation that are needed for JVM / ART tests.
106 // Note that even if the native KMP targets are disabled, we still need to compile the C
107 // code for these targets as they are used in JVM / ART tests (via JNI).
108 def requiredNativeTargets = [
109 KonanTarget.ANDROID_ARM32,
110 KonanTarget.ANDROID_ARM64,
111 KonanTarget.ANDROID_X64,
112 KonanTarget.ANDROID_X86,
113 KonanTarget.MACOS_ARM64,
114 KonanTarget.MACOS_X64,
Daniel Santiago Rivera25b92602024-04-17 22:42:32 -0400115 KonanTarget.MINGW_X64,
Yigit Boyarb87ae802023-10-25 08:33:25 -0700116 KonanTarget.LINUX_X64,
Daniel Santiago Rivera23f1ec62024-08-08 11:24:53 +0100117 KonanTarget.LINUX_ARM64,
Yigit Boyarb87ae802023-10-25 08:33:25 -0700118 ].collect { it.INSTANCE } // Use INSTANCE to get object class instance from kotlin
Daniel Santiago Rivera00ffddf2023-12-12 20:50:35 -0500119
Yigit Boyarb87ae802023-10-25 08:33:25 -0700120 // Define C compilation of SQLite (sqlite3.c)
Daniel Santiago Rivera00ffddf2023-12-12 20:50:35 -0500121 def sqliteCompilation = createNativeCompilation("androidXBundledSqlite") {
Yigit Boyarb87ae802023-10-25 08:33:25 -0700122 configureEachTarget { nativeCompilation ->
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500123 // add SQLite header
Yigit Boyarb87ae802023-10-25 08:33:25 -0700124 nativeCompilation.includes.from(
Yigit Boyar20f1ff42024-01-30 16:23:52 +0000125 prepareSqliteSourcesTask.map { it.destinationDirectory }
Yigit Boyarb87ae802023-10-25 08:33:25 -0700126 )
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500127 // add SQLite sources
Yigit Boyarb87ae802023-10-25 08:33:25 -0700128 nativeCompilation.sources.from(
Yigit Boyar20f1ff42024-01-30 16:23:52 +0000129 prepareSqliteSourcesTask.map {
130 fileTree(it.destinationDirectory).matching { include "**/*.c" }
Yigit Boyarb87ae802023-10-25 08:33:25 -0700131 }
132 )
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500133 // add SQLite compile flags
134 nativeCompilation.freeArgs.addAll(SQLITE_COMPILE_FLAGS)
Yigit Boyarb87ae802023-10-25 08:33:25 -0700135 if (nativeCompilation.konanTarget.family == Family.ANDROID) {
136 nativeCompilation.freeArgs.add("-Oz") // optimize for size
137 } else {
138 nativeCompilation.freeArgs.add("-O3") // optimize for speed
139 }
140 }
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500141 configureTargets(requiredNativeTargets)
Yigit Boyarb87ae802023-10-25 08:33:25 -0700142 }
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500143
144 // add SQLite compilation output as an artifact of a producer configuration
Fred Sladkeyd5f3dd32024-01-23 13:50:51 -0500145 artifacts.add(
146 "linuxSharedArchive",
147 sqliteCompilation.sharedArchiveOutputFor(KonanTarget.LINUX_X64.INSTANCE)
148 )
Yigit Boyar20f1ff42024-01-30 16:23:52 +0000149 artifacts.add(
150 "sqliteSources",
151 prepareSqliteSourcesTask.map { it.destinationDirectory }
152 )
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500153
Daniel Santiago Riverace56aa62024-03-15 13:07:33 -0400154 // Define C++ compilation of JNI
Daniel Santiago Rivera25b92602024-04-17 22:42:32 -0400155 def jvmArtJniImplementation = createNativeCompilation("sqliteJni") {
Yigit Boyarb87ae802023-10-25 08:33:25 -0700156 configureEachTarget { nativeCompilation ->
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500157 // add JNI headers as sources
Yigit Boyarb87ae802023-10-25 08:33:25 -0700158 nativeCompilation.addJniHeaders()
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500159 // add SQLite headers
160 nativeCompilation.includes.from(
Yigit Boyar20f1ff42024-01-30 16:23:52 +0000161 prepareSqliteSourcesTask.map {it.destinationDirectory }
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500162 )
163 // add our JNI sources, i.e. the SQLite bindings
164 nativeCompilation.sources.from(
Daniel Santiago Riverabad7ea62024-05-31 10:04:55 -0400165 fileTree("src/jvmAndroidMain/jni").matching { include "**/*.cpp" }
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500166 )
Daniel Santiago Rivera823d197322024-01-02 12:56:53 -0500167 // statically include the output of SQLite compilation
168 nativeCompilation.include(sqliteCompilation)
Daniel Santiago Riveracf121572024-05-29 11:14:42 -0400169 if (nativeCompilation.konanTarget.family == Family.ANDROID) {
170 // b/341639198: Link to Android's Atomic lib, important for older ARM devices
171 nativeCompilation.linkerArgs.add("-latomic")
172 }
Daniel Santiago Riverad4dae962023-12-14 14:33:32 -0500173 if (nativeCompilation.konanTarget.family == Family.OSX) {
174 // KT-57848
175 nativeCompilation.freeArgs.addAll("-Dat_quick_exit=atexit", "-Dquick_exit=exit")
176 }
Yigit Boyarb87ae802023-10-25 08:33:25 -0700177 }
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500178 configureTargets(requiredNativeTargets)
Yigit Boyarb87ae802023-10-25 08:33:25 -0700179 }
180
181 android() {
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500182 addNativeLibrariesToJniLibs(it, jvmArtJniImplementation)
Yigit Boyarb87ae802023-10-25 08:33:25 -0700183 }
Yigit Boyarb87ae802023-10-25 08:33:25 -0700184 ios()
185 jvm() {
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500186 addNativeLibrariesToResources(it, jvmArtJniImplementation)
Yigit Boyarb87ae802023-10-25 08:33:25 -0700187 }
188 linux()
189 mac()
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500190
Yigit Boyarb87ae802023-10-25 08:33:25 -0700191 defaultPlatform(PlatformIdentifier.ANDROID)
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500192
Omar Ismailce79d6f2024-05-14 16:05:13 +0100193 targets.configureEach { target ->
Yigit Boyarb87ae802023-10-25 08:33:25 -0700194 if (target.platformType == KotlinPlatformType.native) {
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500195 // Configure this native target to the SQLite compilation.
Yigit Boyarb87ae802023-10-25 08:33:25 -0700196 // This list likely only adds the iOS targets when they are enabled as the desktop
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500197 // targets are already part of the required targets to run JVM / ART tests.
Yigit Boyarb87ae802023-10-25 08:33:25 -0700198 sqliteCompilation.configureTarget(target.konanTarget)
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500199 // Create cinterop code for this native target from the SQLite compilation.
Yigit Boyarb87ae802023-10-25 08:33:25 -0700200 createCinterop(target, sqliteCompilation)
201 }
202 }
203
204 sourceSets {
205 // commonMain
206 // ┌───────────────────────────────────────────┐
207 // ▼ ▼
Daniel Santiago Riverabad7ea62024-05-31 10:04:55 -0400208 // jvmAndroidMain nativeMain
Yigit Boyarb87ae802023-10-25 08:33:25 -0700209 // ┌────────────────┐ ┌──────────┬────────────┐
210 // │ │ │ │ │
211 // ▼ ▼ ▼ ▼ ▼
212 // jvmMain androidMain macMain linuxMain iosMain
Yigit Boyarb87ae802023-10-25 08:33:25 -0700213 commonMain {
214 dependencies {
215 implementation(libs.kotlinStdlib)
Daniel Santiago Rivera00ffddf2023-12-12 20:50:35 -0500216 api(project(":sqlite:sqlite"))
Yigit Boyarb87ae802023-10-25 08:33:25 -0700217 }
218 }
Daniel Santiago Riverabad7ea62024-05-31 10:04:55 -0400219 jvmAndroidMain {
Yigit Boyarb87ae802023-10-25 08:33:25 -0700220 dependsOn(commonMain)
221 }
Yigit Boyarb87ae802023-10-25 08:33:25 -0700222 jvmMain {
Daniel Santiago Riverabad7ea62024-05-31 10:04:55 -0400223 dependsOn(jvmAndroidMain)
Yigit Boyarb87ae802023-10-25 08:33:25 -0700224 }
Yigit Boyarb87ae802023-10-25 08:33:25 -0700225 androidMain {
Daniel Santiago Riverabad7ea62024-05-31 10:04:55 -0400226 dependsOn(jvmAndroidMain)
Yigit Boyarb87ae802023-10-25 08:33:25 -0700227 }
Fred Sladkeyd5f3dd32024-01-23 13:50:51 -0500228 nativeMain {
229 dependsOn(commonMain)
230 dependencies {
231 implementation(project(":sqlite:sqlite-framework"))
Yigit Boyarb87ae802023-10-25 08:33:25 -0700232 }
Yigit Boyarb87ae802023-10-25 08:33:25 -0700233 }
Omar Ismailce79d6f2024-05-14 16:05:13 +0100234 targets.configureEach { target ->
Yigit Boyarb87ae802023-10-25 08:33:25 -0700235 if (target.platformType == KotlinPlatformType.native) {
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500236 target.compilations["main"].defaultSourceSet {
Daniel Santiago Rivera00ffddf2023-12-12 20:50:35 -0500237 dependsOn(nativeMain)
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500238 }
Yigit Boyarb87ae802023-10-25 08:33:25 -0700239 }
240 }
241 }
242}
243
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500244android {
Aurimas Liutikas77952822024-12-04 18:00:17 -0800245 namespace = "androidx.sqlite.driver.bundled"
Daniel Santiago Rivera7358b512023-11-16 20:35:59 -0500246}
247
Yigit Boyarb87ae802023-10-25 08:33:25 -0700248androidx {
Daniel Santiago Rivera00ffddf2023-12-12 20:50:35 -0500249 name = "SQLite Bundled Integration"
Omar Ismail86e66062024-05-03 16:10:50 +0100250 type = LibraryType.PUBLISHED_LIBRARY
Yigit Boyarb87ae802023-10-25 08:33:25 -0700251 inceptionYear = "2023"
Daniel Santiago Rivera00ffddf2023-12-12 20:50:35 -0500252 description = "The implementation of SQLite library using the bundled SQLite."
Daniel Santiago Rivera6e1e6e12024-12-16 22:13:20 -0500253 kotlinTarget = KotlinTarget.KOTLIN_2_0
Yigit Boyarb87ae802023-10-25 08:33:25 -0700254}