Skip to content

Commit 72eb464

Browse files
committed
WIP: Add custom jlink/jshell example
gradle secp-examples-jshell:jlink` should build an image with a jshell image that contains libsecp256k1 and supports the following (for example) import module org.bitcoinj.secp var secp = Secp256k1.get() This will load the libsecp256k1-ffm implementation.
1 parent 8ad4a61 commit 72eb464

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

secp-examples-jshell/build.gradle

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import org.codehaus.groovy.runtime.GStringImpl
2+
3+
plugins {
4+
id 'java'
5+
id 'application'
6+
id 'org.beryx.jlink' version '3.1.4-rc'
7+
}
8+
9+
ext.moduleName = 'org.bitcoinj.secp.jshell'
10+
ext.libSecpDir = System.getenv("LIBSECP_DIR")
11+
ext.javaHome = System.getenv("JAVA_HOME")
12+
13+
dependencies {
14+
implementation project(':secp-api')
15+
implementation project(':secp-bouncy')
16+
implementation project(':secp-ffm')
17+
}
18+
19+
tasks.withType(JavaCompile).configureEach {
20+
// Override Default release version
21+
options.release = 25
22+
}
23+
24+
jar {
25+
inputs.property("moduleName", moduleName)
26+
manifest {
27+
attributes 'Implementation-Title': 'Placeholder App for secp256k1-jdk JShell Example',
28+
'Main-Class': application.mainClass,
29+
'Implementation-Version': archiveVersion.get()
30+
}
31+
}
32+
33+
application {
34+
mainModule = 'org.bitcoinj.secp.jshell'
35+
mainClass = 'org.bitcoinj.secp.jshell.Hello'
36+
applicationName = 'hello'
37+
}
38+
39+
run {
40+
jvmArgs += '--enable-native-access=org.bitcoinj.secp.ffm'
41+
}
42+
43+
jlink {
44+
options = ['--add-modules', 'org.bitcoinj.secp.ffm',
45+
'--add-modules', 'org.bitcoinj.secp.bouncy', '--ignore-signing-information',
46+
'--add-modules', 'jdk.jshell']
47+
launcher {
48+
name = application.applicationName
49+
}
50+
}
51+
52+
tasks.register('copyNativeLibraries', Copy) {
53+
dependsOn tasks.jlink
54+
from(libSecpDir) { include 'libsecp256k1.*' }
55+
into("${tasks.jlink.imageDir}/lib")
56+
}
57+
58+
tasks.register('copyJdkSource', Copy) {
59+
dependsOn tasks.jlink
60+
from {
61+
def javaHome = System.getenv('JAVA_HOME') ?: System.getProperty('java.home')
62+
file("$javaHome/lib/src.zip")
63+
}
64+
into("${tasks.jlink.imageDir}/lib")
65+
finalizedBy(tasks.named('addJshellWrapper'))
66+
}
67+
68+
tasks.register('addJshellWrapper') {
69+
dependsOn tasks.copyJdkSource
70+
doLast {
71+
def jshellScript = file("${tasks.jlink.imageDir}/bin/secp-shell")
72+
jshellScript.text = """#!/bin/bash
73+
DIR=\$(dirname "\$0")
74+
"\$DIR/jshell" "\$@"
75+
"""
76+
jshellScript.setExecutable(true)
77+
}
78+
}
79+
80+
tasks.jlink {
81+
finalizedBy(tasks.copyNativeLibraries, tasks.copyJdkSource) // tasks.copyProjectJavadoc
82+
}
83+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2023-2025 secp256k1-jdk Developers.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
/**
17+
* JShell Example.
18+
*/
19+
module org.bitcoinj.secp.jshell {
20+
requires org.bitcoinj.secp;
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright 2023-2025 secp256k1-jdk Developers.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.bitcoinj.secp.jshell;
17+
18+
public class Hello {
19+
void main() {
20+
IO.println("Hello! Try running the `secp-shell` executable for JShell with secp256k1-jdk built-in.");
21+
}
22+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright 2023-2025 secp256k1-jdk Developers.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
/// Contains a placeholder application for a custom JShell build.
17+
package org.bitcoinj.secp.jshell;

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ include 'secp-graalvm' // GraalVM Foreign Function Registration suppor
77
include 'secp-bitcoinj' // bitcoinj integration utilities and tests (P2TR address generation, etc.)
88
include 'secp-integration-test' // Integration tests of API implementations (ffm and bouncy)
99
include 'secp-examples-java' // Java examples
10+
include 'secp-examples-jshell' // jshell example
1011
include 'secp-examples-kotlin' // Kotlin examples
1112

0 commit comments

Comments
 (0)