~breakfastquay/rubberband#25: 
java jar handling could use builtin meson functions

See the following two-part patch, which tries to add java to the languages, and then uses Meson's builtin jar handler.

# HG changeset patch
# User Eli Schwartz <eschwartz93@gmail.com>
# Date 1665102481 14400
#      Thu Oct 06 20:28:01 2022 -0400
# Node ID e00fac5dbb764dc4fe533b444beb2932d30ad4b4
# Parent  16302890e6b1c51498a98c82f03270cf6c579e2b
meson: use builtin add_languages to detect if jni is findable

diff -r 16302890e6b1 -r e00fac5dbb76 meson.build
--- a/meson.build	Fri Sep 30 14:53:53 2022 +0100
+++ b/meson.build	Thu Oct 06 20:28:01 2022 -0400
@@ -128,14 +128,12 @@
                                prefix: '#define _GNU_SOURCE\n#include <math.h>',
                                args: '-lm')
 
-javac = find_program('javac', required: false)
-jar = find_program('jar', required: false)
-
-# Look for JNI only if javac and jar are found. This is because the
+have_java = add_languages('java', required: false)
+# Look for JNI only if javac language is found. This is because the
 # auto-JNI-dependency module in Meson 0.62 and 0.63 appears to bail
 # out of the build completely if it can't find javac, even when
 # required is false
-if javac.found() and jar.found()
+if have_java
   jni_dep = dependency('jni', version: '>= 7.0.0', required: false)
   if not jni_dep.found()
     if cpp.has_header('jni.h', args: extra_include_args)
@@ -144,8 +142,8 @@
   endif
 else
   # Declare jni_dep so it isn't totally undefined - but it won't be
-  # used below because we are conditional on javac/jar as well
-  jni_dep = declare_dependency()
+  # used below because we are conditional on it being found
+  jni_dep = dependency('', required: false)
 endif
 
 
@@ -507,7 +505,7 @@
   #
   if get_option('default_library') != 'shared'
     rubberband_additional_static_lib = 'rubberband-static'
-  endif    
+  endif
   rubberband_library_name = 'rubberband'
   rubberband_program_name = 'rubberband-program'
   rubberband_program_name_r3 = 'rubberband-program-r3'
@@ -567,7 +565,7 @@
   message('Will build Rubber Band Library dynamic library')
   target_summary += { 'Dynamic library': [ true, 'Name: ' + rubberband_library_name ] }
 endif
-  
+
 rubberband_library = library(
   rubberband_library_name,
   # We would like to write "link_with: rubberband_objlib",
@@ -601,7 +599,7 @@
   )
 endif
 
-if jni_dep.found() and javac.found() and jar.found()
+if jni_dep.found()
   target_summary += { 'JNI library': [ true, 'Name: ' + rubberband_jni_name ] }
   message('Will build Java Native Interface')
   rubberband_jni = shared_library(
@@ -622,12 +620,14 @@
     # NB the JNI library is not versioned
     install: true,
   )
+  javac = find_program('javac')
   rubberband_class = custom_target(
     'rubberband_class',
     input: 'com/breakfastquay/rubberband/RubberBandStretcher.java',
     output: 'RubberBandStretcher.class',
     command: [ javac, '@INPUT@', '-d', '@OUTDIR@' ],
   )
+  jar = find_program('jar')
   rubberband_jar = custom_target(
     'rubberband_jar',
     input: rubberband_class,
@@ -637,7 +637,7 @@
   )
 else
   target_summary += { 'JNI library': false }
-  if not (javac.found() and jar.found())
+  if not have_java
     message('Not building Java Native Interface: Java compiler or archiver missing')
   else
     message('Not building Java Native Interface: JNI header not found')
# HG changeset patch
# User Eli Schwartz <eschwartz93@gmail.com>
# Date 1665102569 14400
#      Thu Oct 06 20:29:29 2022 -0400
# Node ID 7b4f49a01fc7b8c3bbe3d203e16bb5f5634d0c7e
# Parent  e00fac5dbb764dc4fe533b444beb2932d30ad4b4
meson: use builtin jar function to compile jar file

It's simpler, and handles rebuild outputs for the class automatically.

diff -r e00fac5dbb76 -r 7b4f49a01fc7 meson.build
--- a/meson.build	Thu Oct 06 20:28:01 2022 -0400
+++ b/meson.build	Thu Oct 06 20:29:29 2022 -0400
@@ -620,21 +620,7 @@
     # NB the JNI library is not versioned
     install: true,
   )
-  javac = find_program('javac')
-  rubberband_class = custom_target(
-    'rubberband_class',
-    input: 'com/breakfastquay/rubberband/RubberBandStretcher.java',
-    output: 'RubberBandStretcher.class',
-    command: [ javac, '@INPUT@', '-d', '@OUTDIR@' ],
-  )
-  jar = find_program('jar')
-  rubberband_jar = custom_target(
-    'rubberband_jar',
-    input: rubberband_class,
-    output: 'rubberband.jar',
-    command: [ jar, 'cvf', '@OUTPUT@', 'com/breakfastquay/rubberband/@INPUT@' ],
-    build_by_default: true,
-  )
+  jar('rubberband', 'com/breakfastquay/rubberband/RubberBandStretcher.java')
 else
   target_summary += { 'JNI library': false }
   if not have_java
Status
RESOLVED CLOSED
Submitter
~eschwartz
Assigned to
No-one
Submitted
5 months ago
Updated
4 months ago
Labels
No labels applied.

~cannam 5 months ago

Thanks for this, and for the added info at https://github.com/breakfastquay/rubberband/issues/73#issuecomment-1275612223 - I will give this a try and see how it fares in the CI environments.

~eschwartz 4 months ago

Awesome, happy to help.

(Late response due to holiday stuff going on.)

~cannam REPORTED CLOSED 4 months ago

Thank you! It's in the 3.1.1 release now.

Register here or Log in to comment, or comment via email.