diff --git a/.gitignore b/.gitignore index 8eb27213b4..65c9d83ebd 100644 --- a/.gitignore +++ b/.gitignore @@ -162,5 +162,6 @@ photonlib-cpp-examples/*/networktables.json.bck photonlib-java-examples/*/networktables.json.bck *.sqlite photon-server/src/main/resources/web/index.html +photon-lib/src/generate/native/cpp/PhotonVersion.cpp venv diff --git a/photon-lib/build.gradle b/photon-lib/build.gradle index 9e76b6cceb..07aba286d0 100644 --- a/photon-lib/build.gradle +++ b/photon-lib/build.gradle @@ -28,7 +28,7 @@ model { sources { cpp { source { - srcDirs 'src/main/native/cpp', "$buildDir/generated/source/proto/main/cpp" + srcDirs 'src/main/native/cpp', "$buildDir/generated/source/proto/main/cpp", "src/generate/native/cpp" include '**/*.cpp', '**/*.cc' } exportedHeaders { @@ -164,8 +164,8 @@ task writeCurrentVersion { def versionFileIn = file("${rootDir}/shared/PhotonVersion.java.in") writePhotonVersionFile(versionFileIn, Path.of("$projectDir", "src", "main", "java", "org", "photonvision", "PhotonVersion.java"), versionString) - versionFileIn = file("${rootDir}/shared/PhotonVersion.h.in") - writePhotonVersionFile(versionFileIn, Path.of("$projectDir", "src", "generate", "native", "include", "PhotonVersion.h"), + versionFileIn = file("${rootDir}/shared/PhotonVersion.cpp.in") + writePhotonVersionFile(versionFileIn, Path.of("$projectDir", "src", "generate", "native", "cpp", "PhotonVersion.cpp"), versionString) } diff --git a/photon-lib/publish.gradle b/photon-lib/publish.gradle new file mode 100644 index 0000000000..fe1048e11d --- /dev/null +++ b/photon-lib/publish.gradle @@ -0,0 +1,199 @@ +apply plugin: 'maven-publish' + +ext.licenseFile = files("$rootDir/LICENSE") + +def outputsFolder = file("$buildDir/outputs") +def allOutputsFolder = file("$buildDir/allOutputs") + +def versionFile = file("$allOutputsFolder/version.txt") + +task outputVersions() { + description = 'Prints the versions of wpilib to a file for use by the downstream packaging project' + group = 'Build' + outputs.files(versionFile) + + doFirst { + buildDir.mkdir() + outputsFolder.mkdir() + allOutputsFolder.mkdir() + } + + doLast { + versionFile.write pubVersion + } +} + +task libraryBuild() {} + +build.dependsOn outputVersions + +task copyAllOutputs(type: Copy) { + destinationDir allOutputsFolder +} + +build.dependsOn copyAllOutputs +copyAllOutputs.dependsOn outputVersions + +ext.addTaskToCopyAllOutputs = { task -> + copyAllOutputs.dependsOn task + copyAllOutputs.inputs.file task.archivePath + copyAllOutputs.from task.archivePath +} + +def artifactGroupId = 'org.photonvision' +def baseArtifactId = 'PhotonLib' +def zipBaseName = "_GROUP_org_photonvision_photonlib_ID_${baseArtifactId}-cpp_CLS" +def javaBaseName = "_GROUP_org_photonvision_photonlib_ID_${baseArtifactId}-java_CLS" + +task cppHeadersZip(type: Zip) { + destinationDirectory = outputsFolder + archiveBaseName = zipBaseName + classifier = "headers" + + duplicatesStrategy = "warn" + + from(licenseFile) { + into '/' + } + + from('src/main/native/include/') { + into '/' + } +} + +task cppSourcesZip(type: Zip) { + destinationDirectory = outputsFolder + archiveBaseName = zipBaseName + classifier = "sources" + + from(licenseFile) { + into '/' + } + + from('src/main/native/cpp') { + into '/' + } +} + +build.dependsOn cppHeadersZip +addTaskToCopyAllOutputs(cppHeadersZip) +build.dependsOn cppSourcesZip +addTaskToCopyAllOutputs(cppSourcesZip) + +task sourcesJar(type: Jar, dependsOn: classes) { + classifier = 'sources' + from sourceSets.main.allSource +} + +task javadocJar(type: Jar, dependsOn: javadoc) { + classifier = 'javadoc' + from javadoc.destinationDir +} + +task outputJar(type: Jar, dependsOn: classes) { + archiveBaseName = javaBaseName + destinationDirectory = outputsFolder + from sourceSets.main.output +} + +task outputSourcesJar(type: Jar, dependsOn: classes) { + archiveBaseName = javaBaseName + destinationDirectory = outputsFolder + classifier = 'sources' + from sourceSets.main.allSource +} + +task outputJavadocJar(type: Jar, dependsOn: javadoc) { + archiveBaseName = javaBaseName + destinationDirectory = outputsFolder + classifier = 'javadoc' + from javadoc.destinationDir +} + +def vendorJson = artifacts.add('archives', file("$photonlibFileOutput")) + +artifacts { + archives sourcesJar + archives javadocJar + archives outputJar + archives outputSourcesJar + archives outputJavadocJar +} + +addTaskToCopyAllOutputs(outputSourcesJar) +addTaskToCopyAllOutputs(outputJavadocJar) +addTaskToCopyAllOutputs(outputJar) + +build.dependsOn outputSourcesJar +build.dependsOn outputJavadocJar +build.dependsOn outputJar + +libraryBuild.dependsOn build + +def releasesRepoUrl = "$buildDir/repos/releases" + +publishing { + repositories { + maven { + url = releasesRepoUrl + } + maven { + url ('https://maven.photonvision.org/repository/' + (isDev ? 'snapshots' : 'internal')) + credentials { + username 'ghactions' + password System.getenv("ARTIFACTORY_API_KEY") + } + } + } +} + +tasks.withType(PublishToMavenRepository) { + doFirst { + println("Publishing to " + repository.url); + } +} + +task cleanReleaseRepo(type: Delete) { + delete releasesRepoUrl +} + +tasks.matching {it != cleanReleaseRepo}.all {it.dependsOn cleanReleaseRepo} + +model { + publishing { + def taskList = createComponentZipTasks($.components, ['Photon'], zipBaseName, Zip, project, includeStandardZipFormat) + + publications { + cpp(MavenPublication) { + taskList.each { + artifact it + } + artifact cppHeadersZip + artifact cppSourcesZip + + artifactId = "${baseArtifactId}-cpp" + groupId artifactGroupId + version pubVersion + } + java(MavenPublication) { + artifact jar + artifact sourcesJar + artifact javadocJar + + artifactId = "${baseArtifactId}-java" + groupId artifactGroupId + version pubVersion + } + vendorjson(MavenPublication) { + artifact vendorJson + + artifactId = "${baseArtifactId}-json" + groupId = artifactGroupId + version "1.0" + } + } + } +} + +publishToMavenLocal.dependsOn libraryBuild +publish.dependsOn libraryBuild diff --git a/photon-lib/src/main/native/cpp/photon/PhotonCamera.cpp b/photon-lib/src/main/native/cpp/photon/PhotonCamera.cpp index 281351a318..91c5c7a5c0 100644 --- a/photon-lib/src/main/native/cpp/photon/PhotonCamera.cpp +++ b/photon-lib/src/main/native/cpp/photon/PhotonCamera.cpp @@ -193,6 +193,22 @@ std::optional PhotonCamera::GetDistCoeffs() { return std::nullopt; } +static bool VersionMatches(std::string them_str) { + std::smatch match; + std::regex versionPattern{"v[0-9]+.[0-9]+.[0-9]+"}; + + std::string us_str = PhotonVersion::versionString; + + // Check that both versions are in the right format + if (std::regex_search(us_str, match, versionPattern) && + std::regex_search(them_str, match, versionPattern)) { + // If they are, check string equality + return (us_str == them_str); + } else { + return false; + } +} + void PhotonCamera::VerifyVersion() { if (!PhotonCamera::VERSION_CHECK_ENABLED) { return; diff --git a/photon-lib/src/main/native/include/PhotonVersion.h b/photon-lib/src/main/native/include/PhotonVersion.h new file mode 100644 index 0000000000..4a95048069 --- /dev/null +++ b/photon-lib/src/main/native/include/PhotonVersion.h @@ -0,0 +1,36 @@ +/* + * MIT License + * + * Copyright (c) PhotonVision + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#pragma once + +#include +#include + +namespace photon { +namespace PhotonVersion { +extern const char* versionString; +extern const char* buildDate; +extern const bool isRelease; +} // namespace PhotonVersion +} // namespace photon diff --git a/photon-lib/src/test/native/cpp/VersionTest.cpp b/photon-lib/src/test/native/cpp/VersionTest.cpp new file mode 100644 index 0000000000..4e547f6a37 --- /dev/null +++ b/photon-lib/src/test/native/cpp/VersionTest.cpp @@ -0,0 +1,32 @@ +/* + * MIT License + * + * Copyright (c) PhotonVision + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include + +#include "PhotonVersion.h" +#include "gtest/gtest.h" + +TEST(VersionTest, PrintVersion) { + std::cout << photon::PhotonVersion::versionString << std::endl; +} diff --git a/shared/PhotonVersion.h.in b/shared/PhotonVersion.cpp.in similarity index 59% rename from shared/PhotonVersion.h.in rename to shared/PhotonVersion.cpp.in index 647421dcda..9b6f84dd00 100644 --- a/shared/PhotonVersion.h.in +++ b/shared/PhotonVersion.cpp.in @@ -23,23 +23,12 @@ * regenerated any time the publish task is run, or when this file is deleted. */ +static const char* dev_ = "dev"; + namespace photon { namespace PhotonVersion { - const std::string versionString = "${version}"; - const std::string buildDate = "${date}"; - const bool isRelease = !(versionString.rfind("dev", 0) == 0); - } - - bool VersionMatches(const std::string& other) { - std::smatch match; - std::regex versionPattern{"v[0-9]+.[0-9]+.[0-9]+"}; - // Check that both versions are in the right format - if (std::regex_search(PhotonVersion::versionString, match, versionPattern) && - std::regex_search(other, match, versionPattern)) { - // If they are, check string equality - return (PhotonVersion::versionString == other); - } else { - return false; - } + const char* versionString = "${version}"; + const char* buildDate = "${date}"; + const bool isRelease = strncmp(dev_, versionString, strlen(dev_)) != 0; } }