Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move PhotonVersion to C++ file #949

Merged
merged 4 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions photon-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand Down
199 changes: 199 additions & 0 deletions photon-lib/publish.gradle
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions photon-lib/src/main/native/cpp/photon/PhotonCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,22 @@ std::optional<cv::Mat> 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;
Expand Down
36 changes: 36 additions & 0 deletions photon-lib/src/main/native/include/PhotonVersion.h
Original file line number Diff line number Diff line change
@@ -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 <regex>
#include <string>

namespace photon {
namespace PhotonVersion {
extern const char* versionString;
extern const char* buildDate;
extern const bool isRelease;
} // namespace PhotonVersion
} // namespace photon
32 changes: 32 additions & 0 deletions photon-lib/src/test/native/cpp/VersionTest.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>

#include "PhotonVersion.h"
#include "gtest/gtest.h"

TEST(VersionTest, PrintVersion) {
std::cout << photon::PhotonVersion::versionString << std::endl;
}
21 changes: 5 additions & 16 deletions shared/PhotonVersion.h.in → shared/PhotonVersion.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Loading