Skip to content

Commit

Permalink
Fix LibraryResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
JKamsker committed May 3, 2024
1 parent 9dc5734 commit 27c41a3
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 21 deletions.
12 changes: 7 additions & 5 deletions .build/build-go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if [ ! -f "$SCRIPT_DIR/tmp/xgo" ]; then
else
echo "xgo already downloaded"
fi

# exit;
# delete $ROOT_DIR/.bin/go/tmp if exists
if [ -d "$ROOT_DIR/.bin/go/tmp" ]; then
rm -rf $ROOT_DIR/.bin/go/tmp
Expand All @@ -33,7 +33,9 @@ fi
mkdir -p $ROOT_DIR/.bin/go/tmp/output
cd $ROOT_DIR/.bin/go/tmp/output

$SCRIPT_DIR/tmp/xgo -buildmode=c-shared $SRC_DIR
# does not work, just for debug
# $SCRIPT_DIR/tmp/xgo -buildmode=c-shared -targets windows/386 $SRC_DIR
$SCRIPT_DIR/tmp/xgo -buildmode=c-shared -targets windows/amd64 $SRC_DIR
# END BUILD


Expand All @@ -46,10 +48,10 @@ mkdir -p $OUTPUT_DIR

# Loop through the files in the current directory
for file in output/*; do
# echo $file
echo $file

# Extract the platform and architecture from the filename using regular expressions
if [[ $file =~ minifier-bindings-(darwin|linux|windows)-(amd64|arm64|ppc64le|riscv64|s390x).* ]]; then
if [[ $file =~ minifier-bindings-(darwin|linux|windows)-(amd64|arm64|ppc64le|riscv64|s390x|386).* ]]; then
platform=${BASH_REMATCH[1]}
architecture=${BASH_REMATCH[2]}

Expand All @@ -65,4 +67,4 @@ done

mv lib $OUTPUT_DIR/..

# rm -rf $ROOT_DIR/.bin/go/tmp
rm -rf $ROOT_DIR/.bin/go/tmp
25 changes: 25 additions & 0 deletions .github/workflows/build-go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Github action configuration which cross compiles "src\minify-go\minify.go" to all available os/archs
# and places the binaries in "bin\minify-go"
name: Build Minify-Go

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: 1.15
- name: Cross compile minify-go
run: |
go get github.com/mitchellh/gox
gox -os="linux darwin windows" -arch="386 amd64" -output="bin/minify-go/minify_{{.OS}}_{{.Arch}}" src/minify-go/minify.go
38 changes: 34 additions & 4 deletions src/minify-go/minify.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,51 @@ import "C"
func init() {
}

func goBytes(str *C.char, length C.longlong) []byte {
return (*[1 << 32]byte)(unsafe.Pointer(str))[:length:length]
}
// func goBytes(str *C.char, length C.longlong) []byte {
// return (*[1 << 32]byte)(unsafe.Pointer(str))[:length:length]
// }

// func goBytes(str *C.char, length C.longlong) []byte {
// return (*[1 << 30]byte)(unsafe.Pointer(str))[:length:length]
// }

// Compiles, but doesn't work
// func goBytes(str *C.char, length C.longlong) []byte {
// return (*(*[]byte)(unsafe.Pointer(str)))[:length:length]
// }

// func goBytes(str *C.char, length C.longlong) []byte {
// _ = (*(*[]*C.char)(unsafe.Pointer(str)))[:length:length]

// return (*[1 << 32]byte)(unsafe.Pointer(str))[:length:length]
// }

// func goStringArray(carr **C.char, length C.longlong) []string {
// if length == 0 {
// return []string{}
// }

// strs := make([]string, length)
// // arr := (*[1 << 32]*C.char)(unsafe.Pointer(carr))[:length:length]
// arr := (*(*[]C.char)(unsafe.Pointer(carr)))[:length:length]
// for i := 0; i < int(length); i++ {
// strs[i] = C.GoString(arr[i])
// }
// return strs
// }

func goBytes(str *C.char, length C.longlong) []byte {
return (*(*[]byte)(unsafe.Pointer(str)))[:length:length]
}

func goStringArray(carr **C.char, length C.longlong) []string {
if length == 0 {
return []string{}
}

strs := make([]string, length)
arr := (*[1 << 32]*C.char)(unsafe.Pointer(carr))[:length:length]
//arr := (*[1 << 32]*C.char)(unsafe.Pointer(carr))[:length:length]
arr := (*(*[]*C.char)(unsafe.Pointer(carr)))[:length:length]
for i := 0; i < int(length); i++ {
strs[i] = C.GoString(arr[i])
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x86;x64</Platforms>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/minify-net/ConsoleTest/NMinify.ConsoleTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ static void Main(string[] args)
{
var text = "<span style=\"color:#ff0000;\" class=\"text\">Some text</span>";
var minifier = new Minifier();
var minified = minifier.MinifyString("text/html", text);
var minified = minifier.MinifyString(MinifierMediaType.Html, text);

Console.WriteLine(text);
Console.WriteLine(minified);
Expand Down
52 changes: 44 additions & 8 deletions src/minify-net/Core/Interop/LibraryResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class LibraryResolver
{
public static Func<IEnumerable<string>> LibraryDirectoryResolver = GetLibraryDirectories;

static Dictionary<(Assembly assembly, DllImportSearchPath? searchPath), nint> _loadedLibraries
static Dictionary<(Assembly assembly, DllImportSearchPath? searchPath), nint> _loadedLibraries
= new();

private static bool _registered;
Expand All @@ -29,7 +29,7 @@ public static void Register()
_registered = true;
NativeLibrary.SetDllImportResolver(typeof(LibraryResolver).Assembly, Resolve);
//NativeLibrary.SetDllImportResolver(Assembly.GetExecutingAssembly(), Resolve);

}

private static nint Resolve(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
Expand All @@ -56,6 +56,9 @@ private static nint ResolveMinifier(Assembly assembly, DllImportSearchPath? sear
return nint.Zero;
}

// #if DEBUG
// Console.WriteLine($"Loading {lib}");
// #endif
return NativeLibrary.Load(lib, assembly, searchPath);
}

Expand All @@ -71,7 +74,16 @@ private static IEnumerable<string> GetLibraryDirectories()
.Prepend(Environment.CurrentDirectory)
.Distinct();

return directories.Concat(directories.Select(n => Path.Combine(n, "..\\..\\..\\..\\..\\..\\..\\.bin\\go\\lib\\")));
return directories.Concat(directories.Select(n =>
{
var root = GetRootPath(n);
if (!string.IsNullOrEmpty(root))
{
return Path.Combine(root, ".bin\\go\\lib\\");
}

return Path.Combine(n, "..\\..\\..\\..\\..\\..\\..\\.bin\\go\\lib\\");
}));

static IEnumerable<string> GetDirectories(params string?[] files)
{
Expand All @@ -83,17 +95,37 @@ static IEnumerable<string> GetDirectories(params string?[] files)
}
}

// Root is marked by a .gitignore file, a .bin file and a .build file. Go up until those files are there.
private static string? GetRootPath(string someSubChild)
{
var root = Path.GetFullPath(someSubChild);
while (!File.Exists(Path.Combine(root, ".gitignore"))
|| !Directory.Exists(Path.Combine(root, ".bin"))
|| !Directory.Exists(Path.Combine(root, ".build")))
{
root = Path.GetDirectoryName(root);
if (string.IsNullOrEmpty(root))
{
return null;
}
}
return root;
}



public static string? ResolveLibraryPath(IEnumerable<string> basePaths)
{
string relativeLibraryPath = GetRelativeLibraryPath();

return basePaths
.Select(x => Path.Combine(x, relativeLibraryPath))
.FirstOrDefault(x => File.Exists(x));
var paths = Enumerable.Concat
(
basePaths.Select(x => Path.Combine(x, relativeLibraryPath)),
basePaths.Select(x => Path.Combine(x, "runtimes", relativeLibraryPath))
);

// Return the path to the library file
//return Path.Combine(libraryPath ?? Directory.GetCurrentDirectory(), relativeLibraryPath);
return paths
.FirstOrDefault(x => File.Exists(x));
}


Expand Down Expand Up @@ -159,6 +191,10 @@ private static string GetBitSystem()
{
return "arm64";
}
else if (RuntimeInformation.ProcessArchitecture == Architecture.X86)
{
return "386";
}
else
{
throw new Exception("Unsupported bit system");
Expand Down
22 changes: 21 additions & 1 deletion src/minify-net/NMinify.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,42 @@ VisualStudioVersion = 17.4.33205.214
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NMinify", "Core\NMinify.csproj", "{BF836001-D6E9-4D80-B249-FD8DF1491E8B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NMinify.ConsoleTest", "ConsoleTest\NMinify.ConsoleTest\NMinify.ConsoleTest.csproj", "{D1E4C592-CA69-4F73-9551-430BEF6A7B5F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NMinify.ConsoleTest", "ConsoleTest\NMinify.ConsoleTest\NMinify.ConsoleTest.csproj", "{D1E4C592-CA69-4F73-9551-430BEF6A7B5F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BF836001-D6E9-4D80-B249-FD8DF1491E8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BF836001-D6E9-4D80-B249-FD8DF1491E8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BF836001-D6E9-4D80-B249-FD8DF1491E8B}.Debug|x64.ActiveCfg = Debug|Any CPU
{BF836001-D6E9-4D80-B249-FD8DF1491E8B}.Debug|x64.Build.0 = Debug|Any CPU
{BF836001-D6E9-4D80-B249-FD8DF1491E8B}.Debug|x86.ActiveCfg = Debug|Any CPU
{BF836001-D6E9-4D80-B249-FD8DF1491E8B}.Debug|x86.Build.0 = Debug|Any CPU
{BF836001-D6E9-4D80-B249-FD8DF1491E8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BF836001-D6E9-4D80-B249-FD8DF1491E8B}.Release|Any CPU.Build.0 = Release|Any CPU
{BF836001-D6E9-4D80-B249-FD8DF1491E8B}.Release|x64.ActiveCfg = Release|Any CPU
{BF836001-D6E9-4D80-B249-FD8DF1491E8B}.Release|x64.Build.0 = Release|Any CPU
{BF836001-D6E9-4D80-B249-FD8DF1491E8B}.Release|x86.ActiveCfg = Release|Any CPU
{BF836001-D6E9-4D80-B249-FD8DF1491E8B}.Release|x86.Build.0 = Release|Any CPU
{D1E4C592-CA69-4F73-9551-430BEF6A7B5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D1E4C592-CA69-4F73-9551-430BEF6A7B5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D1E4C592-CA69-4F73-9551-430BEF6A7B5F}.Debug|x64.ActiveCfg = Debug|x64
{D1E4C592-CA69-4F73-9551-430BEF6A7B5F}.Debug|x64.Build.0 = Debug|x64
{D1E4C592-CA69-4F73-9551-430BEF6A7B5F}.Debug|x86.ActiveCfg = Debug|x86
{D1E4C592-CA69-4F73-9551-430BEF6A7B5F}.Debug|x86.Build.0 = Debug|x86
{D1E4C592-CA69-4F73-9551-430BEF6A7B5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D1E4C592-CA69-4F73-9551-430BEF6A7B5F}.Release|Any CPU.Build.0 = Release|Any CPU
{D1E4C592-CA69-4F73-9551-430BEF6A7B5F}.Release|x64.ActiveCfg = Release|x64
{D1E4C592-CA69-4F73-9551-430BEF6A7B5F}.Release|x64.Build.0 = Release|x64
{D1E4C592-CA69-4F73-9551-430BEF6A7B5F}.Release|x86.ActiveCfg = Release|x86
{D1E4C592-CA69-4F73-9551-430BEF6A7B5F}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 27c41a3

Please sign in to comment.