Skip to content

Commit 5d8e967

Browse files
committed
Generate build-id.txt + Removed CMake options in CMakePlugin + Refactored TaskFunctions.writeToFile
1 parent 53d6eed commit 5d8e967

File tree

6 files changed

+56
-19
lines changed

6 files changed

+56
-19
lines changed

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ build
88
.externalNativeBuild
99
obj
1010

11+
build-id.txt
12+
1113
*.apk
1214
*.iml
1315

‎app/jni/CMakeLists.txt

+14-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ include("${CMAKE_HOME_DIRECTORY}/cmake/Join.cmake")
1010

1111
set(THIRDPARTY_DIR "${CMAKE_HOME_DIRECTORY}/third_party")
1212
set(STUB_DIR "${CMAKE_HOME_DIRECTORY}/stub")
13-
set(TDLIB_DIR "${CMAKE_HOME_DIRECTORY}/../../tdlib")
13+
set(TGX_ROOT_DIR "${CMAKE_HOME_DIRECTORY}/../..")
14+
set(TDLIB_DIR "${TGX_ROOT_DIR}/tdlib")
1415
set(UTILS_DIR "${THIRDPARTY_DIR}/jni-utils")
1516

1617
set(SSL_DIR "${THIRDPARTY_DIR}/openssl/android_static/${ANDROID_ABI}")
@@ -61,6 +62,7 @@ set(ORIGINAL_CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS}")
6162
set(ADD_COMMON_FLAGS
6263
-w -Werror=return-type
6364
)
65+
6466
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
6567
set(CMAKE_C_VISIBILITY_PRESET hidden)
6668
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
@@ -91,6 +93,17 @@ foreach(lang C CXX)
9193
set(CMAKE_${lang}_FLAGS "${CMAKE_${lang}_FLAGS} ${ADD_${lang}_FLAGS}")
9294
endforeach()
9395

96+
set(CMAKE_SKIP_RPATH ON)
97+
set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
98+
99+
file(READ "${TGX_ROOT_DIR}/build-id.txt" TGX_BUILD_ID)
100+
string(LENGTH "${TGX_BUILD_ID}" TGX_BUILD_ID_LENGTH)
101+
102+
if(${TGX_BUILD_ID_LENGTH} EQUAL 0)
103+
message(FATAL_ERROR "Build-Id is empty or doesn't exist, check: ${TGX_ROOT_DIR}/build-id.txt")
104+
endif()
105+
106+
94107
set(ADD_LINKER_FLAGS
95108
-Wl,--gc-sections,--icf=safe
96109
-Wl,--build-id=none

‎buildSrc/src/main/kotlin/me/vkryl/plugin/CMakePlugin.kt

+1-6
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@ open class CMakePlugin : Plugin<Project> {
3333
cmake {
3434
arguments(
3535
"-DANDROID_STL=c++_shared",
36-
"-DANDROID_PLATFORM=android-${Config.MIN_SDK_VERSION}",
37-
"-DCMAKE_BUILD_WITH_INSTALL_RPATH=ON",
38-
"-DCMAKE_SKIP_RPATH=ON",
39-
"-DCMAKE_C_VISIBILITY_PRESET=hidden",
40-
"-DCMAKE_CXX_VISIBILITY_PRESET=hidden",
41-
"-DCMAKE_BUILD_PARALLEL_LEVEL=${Runtime.getRuntime().availableProcessors()}"
36+
"-DANDROID_PLATFORM=android-${Config.MIN_SDK_VERSION}"
4237
)
4338
}
4439
}

‎buildSrc/src/main/kotlin/me/vkryl/plugin/ModulePlugin.kt

+20
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ import com.android.build.gradle.AppExtension
2222
import com.android.build.gradle.BaseExtension
2323
import com.android.build.gradle.LibraryExtension
2424
import com.android.build.gradle.ProguardFiles
25+
import com.android.build.gradle.internal.cxx.hashing.sha256Of
26+
import getIntOrThrow
2527
import getLongOrThrow
2628
import getOrThrow
2729
import loadProperties
30+
import me.vkryl.task.writeToFile
2831
import monthYears
2932
import org.gradle.api.Plugin
3033
import org.gradle.api.Project
@@ -35,6 +38,7 @@ import org.gradle.kotlin.dsl.get
3538
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
3639
import java.io.File
3740
import java.net.URI
41+
import java.security.MessageDigest
3842
import java.util.*
3943

4044
open class ModulePlugin : Plugin<Project> {
@@ -214,6 +218,22 @@ open class ModulePlugin : Plugin<Project> {
214218
PullRequest(it.toLong(), properties)
215219
}.sortedBy { it.id }
216220

221+
val buildIdContents = commitHashLong + "+" + (
222+
versions.getIntOrThrow("version.jni") +
223+
versions.getIntOrThrow("version.tdlib") +
224+
versions.getIntOrThrow("version.leveldb")
225+
) + (if (pullRequests.isEmpty()) {
226+
""
227+
} else {
228+
"+" + pullRequests.joinToString(",") { it.commitShort }
229+
})
230+
val buildId = sha256Of(
231+
buildIdContents
232+
)
233+
writeToFile("build-id.txt") {
234+
it.append(buildId)
235+
}
236+
217237
namespace = "org.thunderdog.challegram"
218238

219239
defaultConfig {

‎buildSrc/src/main/kotlin/me/vkryl/task/FetchLanguagesTask.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ open class FetchLanguagesTask : BaseTask() {
231231
allFolders.addAll(outputFolders)
232232

233233
for (outputFolder in outputFolders) {
234-
writeToFile("app/src/main/res/values-${outputFolder}/strings.xml", mkdirs = true) { xml ->
234+
writeToFile("app/src/main/res/values-${outputFolder}/strings.xml") { xml ->
235235
xml.append("""
236236
<?xml version="1.0" encoding="utf-8"?>
237237
<!-- AUTOGENERATED, DO NOT MODIFY -->

‎buildSrc/src/main/kotlin/me/vkryl/task/TaskFunctions.kt

+18-11
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,31 @@ import java.io.Writer
1818
import java.nio.channels.FileChannel
1919
import java.nio.file.StandardOpenOption
2020
import java.util.*
21-
import kotlin.contracts.ExperimentalContracts
22-
import kotlin.contracts.InvocationKind
23-
import kotlin.contracts.contract
2421

25-
@ExperimentalContracts
26-
fun writeToFile(path: String, mkdirs: Boolean = true, isRelativePath: Boolean = true, block: (Writer) -> Unit) {
27-
contract {
28-
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
29-
}
22+
fun isWindowsHost(): Boolean {
23+
return System.getProperty("os.name").startsWith("Windows")
24+
}
3025

31-
val isWindows = System.getProperty("os.name").startsWith("Windows")
26+
fun writeToFile(path: String, block: (Writer) -> Unit) {
27+
val isWindows = isWindowsHost()
28+
// TODO proper detection, but it isn't needed for now,
29+
// because all paths passed to this method are relative.
30+
val isRelativePath = !path.startsWith("/")
31+
val isRootFolder = !path.contains("/")
3232
val file = if (isRelativePath && isWindows) {
3333
File("${System.getProperty("user.dir")}${File.separator}$path")
3434
} else {
3535
File(path)
3636
}
37+
writeToFile(file, mkdirs = !isRootFolder, block)
38+
}
3739

38-
if (!file.parentFile.exists()) {
40+
fun writeToFile(file: File, mkdirs: Boolean = true, block: (Writer) -> Unit) {
41+
if (file.parentFile == null) {
42+
if (mkdirs) {
43+
error("Invalid file path: ${file.absolutePath}")
44+
}
45+
} else if (!file.parentFile.exists()) {
3946
if (mkdirs) {
4047
if (!file.parentFile.mkdirs())
4148
error("Could not create folder: ${file.parentFile.absolutePath}")
@@ -62,7 +69,7 @@ fun writeToFile(path: String, mkdirs: Boolean = true, isRelativePath: Boolean =
6269

6370
if (file.exists()) {
6471
if (!areFileContentsIdentical(file, outFile)) {
65-
if (isWindows) {
72+
if (isWindowsHost()) {
6673
Thread.sleep(300)
6774
System.gc()
6875
}

0 commit comments

Comments
 (0)