Skip to content

Commit

Permalink
Support slashes in rule names
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed May 18, 2022
1 parent b975294 commit 4446d48
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
11 changes: 6 additions & 5 deletions jni/internal/cc_jni_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,10 @@ def cc_jni_library(
[`cc_library`](https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library), except for:
`linkshared` (always `True`), `linkstatic` (always `True`), `data` (runfiles are not supported)
"""
macos_library_name = "lib%s.dylib" % name
unix_library_name = "lib%s.so" % name
windows_library_name = "%s.dll" % name
path, sep, basename = name.rpartition("/")
macos_library_name = "%s%slib%s.dylib" % (path, sep, basename)
unix_library_name = "%s%slib%s.so" % (path, sep, basename)
windows_library_name = "%s%s%s.dll" % (path, sep, basename)

# Arguments to set on the visible target, not the intermediate cc_binary.
tags = cc_binary_args.pop("tags", default = None)
Expand All @@ -190,13 +191,13 @@ def cc_jni_library(
java_coverage_helper_name = "%s_java_coverage_helper" % name
java_jni_coverage_helper_library(
name = java_coverage_helper_name,
library_name = name,
library_name = basename,
)

cc_coverage_helper_name = "%s_cc_coverage_helper" % name
cc_jni_coverage_helper_library(
name = cc_coverage_helper_name,
library_name = name,
library_name = basename,
)

# Simple concatenation is compatible with select, append is not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,31 @@ public static void loadLibrary(String name, String absolutePathToPackage) {
}

synchronized private static void loadLibrary(String name, URL libraryResource) {
if (LOADED_LIBS.containsKey(name)) {
if (!libraryResource.toString().equals(LOADED_LIBS.get(name).canonicalPath)) {
String basename = libraryBasename(name);
if (LOADED_LIBS.containsKey(basename)) {
if (!libraryResource.toString().equals(LOADED_LIBS.get(basename).canonicalPath)) {
throw new UnsatisfiedLinkError(String.format(
"Cannot load two native libraries with same basename ('%s') from different paths\nFirst library: %s\nSecond library: %s\n",
name, LOADED_LIBS.get(name).canonicalPath, libraryResource));
basename, LOADED_LIBS.get(basename).canonicalPath, libraryResource));
}
return;
}
try {
Path tempDir = getOrCreateTempDir();
String mappedName = System.mapLibraryName(name);
String mappedName = System.mapLibraryName(basename);
int lastDot = mappedName.lastIndexOf('.');
Path tempFile = Files.createTempFile(
tempDir, mappedName.substring(0, lastDot) + "_", mappedName.substring(lastDot));
LOADED_LIBS.put(name, new NativeLibraryInfo(libraryResource.toString(), tempFile.toFile()));
LOADED_LIBS.put(
basename, new NativeLibraryInfo(libraryResource.toString(), tempFile.toFile()));
try (InputStream in = libraryResource.openStream()) {
Files.copy(in, tempFile, StandardCopyOption.REPLACE_EXISTING);
System.load(tempFile.toAbsolutePath().toString());
}
} catch (IOException e) {
throw new UnsatisfiedLinkError(e.getMessage());
}
CoverageHelper.initCoverage(name);
CoverageHelper.initCoverage(basename);
}

private static Path getOrCreateTempDir() throws IOException {
Expand All @@ -126,12 +128,18 @@ private static Path getOrCreateTempDir() throws IOException {
return tempDir;
}

private static String libraryBasename(String name) {
return name.substring(name.lastIndexOf('/') + 1);
}

private static String libraryRelativePath(String name) {
if (name == null) {
throw new NullPointerException("name must not be null");
}
return String.format("%s_%s_%s/%s", name, OsCpuUtils.CANONICAL_OS, OsCpuUtils.CANONICAL_CPU,
System.mapLibraryName(name));
String basename = libraryBasename(name);
String path = name.substring(0, name.length() - basename.length());
return String.format("%s%s_%s_%s/%s", path, basename, OsCpuUtils.CANONICAL_OS,
OsCpuUtils.CANONICAL_CPU, System.mapLibraryName(basename));
}

private static void failOnNullResource(URL resource, String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ java_jni_library(
"OsUtils.java",
],
native_libs = [
"//native_loader/src/main/native/com/example/os",
"//native_loader/src/main/native/com/example/os:impl/os",
],
visibility = [
"//native_loader/src/main/native/com/example/os:__subpackages__",
Expand Down
4 changes: 2 additions & 2 deletions tests/native_loader/src/main/java/com/example/os/OsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class OsUtils {

static {
String packagePath = OsUtils.class.getPackage().getName().replace(".", "/");
RulesJni.loadLibrary("os", "/" + packagePath);
RulesJni.loadLibrary("impl/os", "/" + packagePath);
// Verify that loading the library twice does not result in errors.
RulesJni.loadLibrary("os", OsUtils.class);
RulesJni.loadLibrary("impl/os", OsUtils.class);
}

public static native int setenv(String name, String value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("@fmeum_rules_jni//jni:defs.bzl", "cc_jni_library")

cc_jni_library(
name = "os",
name = "impl/os",
testonly = True,
srcs = [
"onload.cpp",
Expand Down

0 comments on commit 4446d48

Please sign in to comment.