Files
allwpilib/wpilibcExamples/build.gradle
sciencewhiz 21d921184a [examples] Add compilable code snippets (#7909)
This enables frc-docs to use RLIs for things that are currently in-line
code blocks, and ensures they compile, which is important with the 2027
breaking changes coming. They are kept separate from the examples to
ensure they don't polute the VSCode examples finder.

Adds the Encoder snippets used in the frc-docs Encoder article as the
first instance of this.
2025-04-22 15:26:26 -06:00

412 lines
18 KiB
Groovy

import org.gradle.language.base.internal.ProjectLayout
apply plugin: 'cpp'
apply plugin: 'c'
apply plugin: 'google-test-test-suite'
apply plugin: 'visual-studio'
apply plugin: 'edu.wpi.first.NativeUtils'
apply plugin: ExtraTasks
evaluationDependsOn(':hal')
apply from: '../shared/config.gradle'
apply from: "${rootDir}/shared/googletest.gradle"
ext.examplesMap = [:]
ext.templatesMap = [:]
ext.snippetsMap = [:]
ext {
templateDirectory = new File("$projectDir/src/main/cpp/templates/")
templateFile = new File("$projectDir/src/main/cpp/templates/templates.json")
exampleDirectory = new File("$projectDir/src/main/cpp/examples/")
exampleFile = new File("$projectDir/src/main/cpp/examples/examples.json")
commandDirectory = new File("$projectDir/src/main/cpp/commands/")
commandFile = new File("$projectDir/src/main/cpp/commands/commands.json")
snippetsDirectory = new File("$projectDir/src/main/cpp/snippets/")
snippetsFile = new File("$projectDir/src/main/cpp/snippets/snippets.json")
}
exampleDirectory.list(new FilenameFilter() {
@Override
public boolean accept(File current, String name) {
return new File(current, name).isDirectory();
}
}).each {
examplesMap.put(it, [])
}
templateDirectory.list(new FilenameFilter() {
@Override
public boolean accept(File current, String name) {
return new File(current, name).isDirectory();
}
}).each {
templatesMap.put(it, [])
}
snippetsDirectory.list(new FilenameFilter() {
@Override
public boolean accept(File current, String name) {
return new File(current, name).isDirectory();
}
}).each {
snippetsMap.put(it, [])
}
nativeUtils.platformConfigs.named(nativeUtils.wpi.platforms.windowsx64).configure {
linker.args.remove('/DEBUG:FULL')
cppCompiler.debugArgs.remove('/Zi')
cCompiler.debugArgs.remove('/Zi')
}
ext {
sharedCvConfigs = examplesMap + templatesMap + snippetsMap.collectEntries { key, value -> ['snippets' + key, value] } + [commands: []]
staticCvConfigs = [:]
useJava = false
useCpp = true
}
def simModules = ["halsim_gui"]
apply from: "${rootDir}/shared/opencv.gradle"
model {
components {
commands(NativeLibrarySpec) {
binaries.all { binary ->
if (binary in StaticLibraryBinarySpec) {
binary.buildable = false
return
}
lib project: ':wpilibNewCommands', library: 'wpilibNewCommands', linkage: 'shared'
lib project: ':romiVendordep', library: 'romiVendordep', linkage: 'shared'
lib project: ':xrpVendordep', library: 'xrpVendordep', linkage: 'shared'
lib project: ':apriltag', library: 'apriltag', linkage: 'shared'
lib project: ':wpilibc', library: 'wpilibc', linkage: 'shared'
lib project: ':wpimath', library: 'wpimath', linkage: 'shared'
project(':ntcore').addNtcoreDependency(binary, 'shared')
lib project: ':cscore', library: 'cscore', linkage: 'shared'
project(':hal').addHalDependency(binary, 'shared')
lib project: ':cameraserver', library: 'cameraserver', linkage: 'shared'
lib project: ':wpinet', library: 'wpinet', linkage: 'shared'
lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared'
}
sources {
cpp {
source {
srcDirs = ['src/main/cpp/commands']
include '**/*.cpp'
}
exportedHeaders {
srcDirs 'src/main/cpp/commands'
include '**/*.h'
}
}
}
}
examplesMap.each { key, value ->
"${key}"(NativeExecutableSpec) {
targetBuildTypes 'debug'
binaries.all { binary ->
lib project: ':wpilibNewCommands', library: 'wpilibNewCommands', linkage: 'shared'
lib project: ':romiVendordep', library: 'romiVendordep', linkage: 'shared'
lib project: ':xrpVendordep', library: 'xrpVendordep', linkage: 'shared'
lib project: ':wpilibc', library: 'wpilibc', linkage: 'shared'
lib project: ':apriltag', library: 'apriltag', linkage: 'shared'
lib project: ':wpimath', library: 'wpimath', linkage: 'shared'
project(':ntcore').addNtcoreDependency(binary, 'shared')
lib project: ':cscore', library: 'cscore', linkage: 'shared'
project(':hal').addHalDependency(binary, 'shared')
lib project: ':cameraserver', library: 'cameraserver', linkage: 'shared'
lib project: ':wpinet', library: 'wpinet', linkage: 'shared'
lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared'
if (binary.targetPlatform.name == nativeUtils.wpi.platforms.roborio) {
nativeUtils.useRequiredLibrary(binary, 'ni_link_libraries', 'ni_runtime_libraries')
}
if (binary.targetPlatform.name == getCurrentArch()) {
simModules.each {
lib project: ":simulation:$it", library: it, linkage: 'shared'
}
}
}
sources {
cpp {
source {
srcDirs 'src/main/cpp/examples/' + "${key}" + "/cpp"
include '**/*.cpp'
}
exportedHeaders {
srcDirs 'src/main/cpp/examples/' + "${key}" + "/include"
include '**/*.h'
}
}
}
sources {
c {
source {
srcDirs 'src/main/cpp/examples/' + "${key}" + "/c"
include '**/*.c'
}
exportedHeaders {
srcDirs 'src/main/cpp/examples/' + "${key}" + "/include"
include '**/*.h'
}
}
}
}
}
templatesMap.each { key, value ->
"${key}"(NativeExecutableSpec) {
targetBuildTypes 'debug'
binaries.all { binary ->
lib project: ':wpilibNewCommands', library: 'wpilibNewCommands', linkage: 'shared'
lib project: ':romiVendordep', library: 'romiVendordep', linkage: 'shared'
lib project: ':xrpVendordep', library: 'xrpVendordep', linkage: 'shared'
lib project: ':wpilibc', library: 'wpilibc', linkage: 'shared'
lib project: ':apriltag', library: 'apriltag', linkage: 'shared'
lib project: ':wpimath', library: 'wpimath', linkage: 'shared'
project(':ntcore').addNtcoreDependency(binary, 'shared')
lib project: ':cscore', library: 'cscore', linkage: 'shared'
project(':hal').addHalDependency(binary, 'shared')
lib project: ':cameraserver', library: 'cameraserver', linkage: 'shared'
lib project: ':wpinet', library: 'wpinet', linkage: 'shared'
lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared'
if (binary.targetPlatform.name == nativeUtils.wpi.platforms.roborio) {
nativeUtils.useRequiredLibrary(binary, 'ni_link_libraries', 'ni_runtime_libraries')
}
}
sources {
cpp {
source {
srcDirs 'src/main/cpp/templates/' + "${key}" + "/cpp"
include '**/*.cpp'
}
exportedHeaders {
srcDirs 'src/main/cpp/templates/' + "${key}" + "/include"
include '**/*.h'
}
}
}
}
}
snippetsMap.each { key, value ->
"snippets${key}"(NativeExecutableSpec) {
targetBuildTypes 'debug'
binaries.all { binary ->
lib project: ':wpilibNewCommands', library: 'wpilibNewCommands', linkage: 'shared'
lib project: ':romiVendordep', library: 'romiVendordep', linkage: 'shared'
lib project: ':xrpVendordep', library: 'xrpVendordep', linkage: 'shared'
lib project: ':wpilibc', library: 'wpilibc', linkage: 'shared'
lib project: ':apriltag', library: 'apriltag', linkage: 'shared'
lib project: ':wpimath', library: 'wpimath', linkage: 'shared'
project(':ntcore').addNtcoreDependency(binary, 'shared')
lib project: ':cscore', library: 'cscore', linkage: 'shared'
project(':hal').addHalDependency(binary, 'shared')
lib project: ':cameraserver', library: 'cameraserver', linkage: 'shared'
lib project: ':wpinet', library: 'wpinet', linkage: 'shared'
lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared'
if (binary.targetPlatform.name == nativeUtils.wpi.platforms.roborio) {
nativeUtils.useRequiredLibrary(binary, 'ni_link_libraries', 'ni_runtime_libraries')
}
if (binary.targetPlatform.name == getCurrentArch()) {
simModules.each {
lib project: ":simulation:$it", library: it, linkage: 'shared'
}
}
}
sources {
cpp {
source {
srcDirs 'src/main/cpp/snippets/' + "${key}" + "/cpp"
include '**/*.cpp'
}
exportedHeaders {
srcDirs 'src/main/cpp/snippets/' + "${key}" + "/include"
include '**/*.h'
}
}
}
sources {
c {
source {
srcDirs 'src/main/cpp/snippets/' + "${key}" + "/c"
include '**/*.c'
}
exportedHeaders {
srcDirs 'src/main/cpp/snippets/' + "${key}" + "/include"
include '**/*.h'
}
}
}
}
}
}
testSuites {
examplesMap.each { key, value ->
def testFolder = new File("${rootDir}/wpilibcExamples/src/test/cpp/examples/${key}")
if (testFolder.exists()) {
"${key}Test"(GoogleTestTestSuiteSpec) {
for (NativeComponentSpec c : $.components) {
if (c.name == key) {
testing c
break
}
}
sources {
cpp {
source {
srcDirs "src/test/cpp/examples/${key}/cpp"
include '**/*.cpp'
}
exportedHeaders {
srcDirs "src/test/cpp/examples/${key}/include"
}
}
c {
source {
srcDirs "src/test/cpp/examples/${key}/c"
include '**/*.c'
}
exportedHeaders {
srcDirs "src/test/cpp/examples/${key}/include"
}
}
}
}
}
}
}
testSuites {
snippetsMap.each { key, value ->
def testFolder = new File("${rootDir}/wpilibcExamples/src/test/cpp/snippets/${key}")
if (testFolder.exists()) {
"snippets${key}Test"(GoogleTestTestSuiteSpec) {
for (NativeComponentSpec c : $.components) {
if (c.name == key) {
testing c
break
}
}
sources {
cpp {
source {
srcDirs "src/test/cpp/examples/${key}/cpp"
include '**/*.cpp'
}
exportedHeaders {
srcDirs "src/test/cpp/examples/${key}/include"
}
}
c {
source {
srcDirs "src/test/cpp/examples/${key}/c"
include '**/*.c'
}
exportedHeaders {
srcDirs "src/test/cpp/examples/${key}/include"
}
}
}
}
}
}
}
binaries {
withType(GoogleTestTestSuiteBinarySpec) {
lib project: ':wpilibNewCommands', library: 'wpilibNewCommands', linkage: 'shared'
lib project: ':romiVendordep', library: 'romiVendordep', linkage: 'shared'
lib project: ':xrpVendordep', library: 'xrpVendordep', linkage: 'shared'
lib project: ':wpilibc', library: 'wpilibc', linkage: 'shared'
lib project: ':apriltag', library: 'apriltag', linkage: 'shared'
lib project: ':wpimath', library: 'wpimath', linkage: 'shared'
project(':ntcore').addNtcoreDependency(it, 'shared')
lib project: ':cscore', library: 'cscore', linkage: 'shared'
project(':hal').addHalDependency(it, 'shared')
lib project: ':cameraserver', library: 'cameraserver', linkage: 'shared'
lib project: ':wpinet', library: 'wpinet', linkage: 'shared'
lib project: ':wpiutil', library: 'wpiutil', linkage: 'shared'
if (it.targetPlatform.name == nativeUtils.wpi.platforms.roborio) {
nativeUtils.useRequiredLibrary(it, 'ni_link_libraries', 'ni_runtime_libraries')
}
nativeUtils.useRequiredLibrary(it, 'opencv_shared')
it.cppCompiler.define('RUNNING_FRC_TESTS')
it.cCompiler.define('RUNNING_FRC_TESTS')
}
}
}
apply from: 'publish.gradle'
model {
tasks {
def c = $.components
project.tasks.register('buildDesktopCpp') { compileTask->
def systemArch = getCurrentArch()
c.each {
if (it in NativeExecutableSpec && it.name) {
it.binaries.each {
def arch = it.targetPlatform.name
if (arch == systemArch && it.buildType.name == 'debug') {
compileTask.dependsOn it.tasks.link
}
}
}
}
}
}
}
model {
// Create run tasks for all examples.
tasks {
// Iterate through the components and check if it is an example.
$.components.each { component ->
if (examplesMap.containsKey(component.name)) {
// Get the appropriate binary and create the run task.
component.binaries.each { binary ->
if (binary.targetPlatform.name == getCurrentArch() && binary.buildType.name == "debug") {
project.tasks.create("run${component.name}", Exec) {
// Add simulation modules to HALSIM_EXTENSIONS environment variable.
def setupEnv = {
String extensions = ""
binary.tasks.install.installDirectory.get().getAsFile().eachFileRecurse {
def name = it.name
// If we don't have a shared library, skip.
if (!(name.endsWith('.dll') || name.endsWith('.so') || name.endsWith('.dylib')))
return
def file = it
simModules.each {
if (name.startsWith(it) || name.startsWith("lib$it".toString())) {
extensions += file.absolutePath + File.pathSeparator
}
}
}
if (extensions != '') {
environment 'HALSIM_EXTENSIONS', extensions
}
}
// Create the task dependency and run the executable.
doFirst { setupEnv() }
dependsOn binary.tasks.install
commandLine binary.tasks.install.runScriptFile.get().asFile.toString()
group = "application"
}
}
}
}
}
}
}
ext {
isCppCommands = true
}
apply from: "${rootDir}/shared/examplecheck.gradle"