This repository has been archived by the owner on May 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpp.gradle
150 lines (124 loc) · 4.65 KB
/
cpp.gradle
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
// https://docs.gradle.org/6.0.1/javadoc/org/gradle/nativeplatform/platform/OperatingSystem.html
import org.gradle.internal.os.OperatingSystem
import java.nio.file.Paths
// https://docs.gradle.org/6.0.1/userguide/building_cpp_projects.html
// https://docs.gradle.org/6.0.1/userguide/cpp_library_plugin.html
// https://docs.gradle.org/6.0.1/userguide/cpp_testing.html
plugins {
id 'cpp-library'
id 'cpp-unit-test'
}
repositories {
jcenter()
}
def currentSystem = OperatingSystem.current()
println "using System: $currentSystem"
def javaDir = "$System.env.JAVA_HOME"
println "using JAVA_HOME: $javaDir"
group "dev.luncliff"
version = '0.4'
// https://docs.gradle.org/6.0.1/dsl/org.gradle.api.tasks.Exec.html
task codegen(type:Exec) {
// change the working directory
workingDir = project.projectDir
println "running 'javac' in $workingDir ..."
commandLine 'javac', '-h', "src/main/headers", \
Paths.get("src/main/java", "dev/luncliff/Module.java"), \
Paths.get("src/main/java", "dev/luncliff/Module2.java")
}
library {
// name of the output binary
baseName = 'eclair'
// [Linkage.STATIC, Linkage.SHARED]
linkage = [Linkage.SHARED]
// https://docs.gradle.org/6.0.1/javadoc/org/gradle/nativeplatform/TargetMachineFactory.html
targetMachines = [
machines.windows.x86_64,
machines.linux.x86_64,
machines.macOS.x86_64
]
// https://docs.gradle.org/6.0.1/userguide/building_cpp_projects.html#sec:custom_cpp_source_set_paths
// https://docs.gradle.org/6.0.1/javadoc/org/gradle/api/file/ConfigurableFileCollection.html
source.from \
file('src/main/cpp')
// plugin currently requires exactly one public header directory
publicHeaders.from \
file('src/main/public')
// private
privateHeaders.from \
file("$javaDir\\include"), \
file("$javaDir\\include\\win32"), file("$javaDir\\include\\darwin"), file("$javaDir\\include\\linux"), \
file('src/main/cpp'), \
file('src/main/headers')
}
// https://docs.gradle.org/6.0.1/dsl/org.gradle.nativeplatform.tasks.LinkSharedLibrary.html
tasks.withType(LinkSharedLibrary).configureEach {
// // glob library in specific folder
// if (currentSystem.isMacOsX()){
// def pattern = currentSystem.getLinkLibraryName('*.dylib')
// def prebuilts = fileTree(dir: "$javaDir\\lib", includes: [pattern])
// for (file in prebuilts.getFiles()){
// println "module: ${file.getName()}"
// }
// libs.from prebuilts
// }
// https://docs.gradle.org/6.0.1/dsl/org.gradle.nativeplatform.platform.NativePlatform.html
linkerArgs.addAll targetPlatform.map { platform ->
if (platform.operatingSystem.isWindows()){
def libname = currentSystem.getLinkLibraryName('ws2_32');
return [libname]
}
return []
}
}
// https://docs.gradle.org/6.0.1/userguide/cpp_library_plugin.html#sec:cpp_library_compile_task
// https://docs.gradle.org/6.0.1/dsl/org.gradle.language.cpp.tasks.CppCompile.html
tasks.withType(CppCompile).configureEach {
println "configure task: ${getName()}"
// CppCompile properties
positionIndependentCode = true
debuggable = true
// compile-time macro
macros.put('DEBUG', '1')
// compiler options
compilerArgs.addAll toolChain.map { NativeToolChain toolChain ->
if (toolChain in Clang) {
return ['-Wall', '-std=c++17', '-stdlib=libc++']
}
if (toolChain in Gcc) {
return ['-Wall', '-std=gnu++17']
}
if (toolChain in VisualCpp) {
// boost.ut requires C++ latest
return ['/W4', '/std:c++latest', '/utf-8']
}
return [] // unknown compiler
}
}
// https://docs.gradle.org/6.0.1/userguide/cpp_unit_test_plugin.html
unitTest {
baseName = 'eclair_test'
targetMachines = [
machines.linux.x86_64,
machines.windows.x86_64,
machines.macOS.x86_64
]
// https://docs.gradle.org/6.0.1/userguide/cpp_unit_test_plugin.html#sec:cpp_unit_test_conventions
source.from file('test/cpp')
privateHeaders.from \
file('externals/ut/include'), \
file('test/cpp')
}
// test executable may have dependency ...
tasks.withType(LinkExecutable).configureEach {
linkerArgs.addAll targetPlatform.map { platform ->
if (platform.operatingSystem.isWindows()){
return [ currentSystem.getLinkLibraryName('ws2_32') ]
}
return []
}
}
// https://docs.gradle.org/6.0.1/dsl/org.gradle.nativeplatform.test.tasks.RunTestExecutable.html
tasks.withType(RunTestExecutable).configureEach {
environment.put("TEST", "true")
}