Skip to content
Open
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,8 @@ The `/pkg` directory contains the Go files converted by c-for-go and the manuall

- `/opt/mxdriver/lib/libmxsml.so`
- `/opt/maca/lib/libmxsml.so`
- `/opt/mxn100/lib/libmxsml.so`
- `/opt/mxn100/lib/libmxsml.so`

For custom container layouts, you can also point the loader to a specific file
or directory with `MXSML_LIBRARY_PATH`, or provide `MACA_HOME` and let the
loader resolve `lib/libmxsml.so` underneath it.
24 changes: 12 additions & 12 deletions gen/License
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
LICENSE
Copyright © 2026 MetaX Integrated Circuits (Shanghai) Co., Ltd.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
LICENSE

Copyright © 2026 MetaX Integrated Circuits (Shanghai) Co., Ltd.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28 changes: 12 additions & 16 deletions pkg/lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
// #include <stdlib.h>
import "C"

const mxsmlLibName = "libmxsml.so"

var g_mxsmlLib = newMxsmlLib()

type library struct {
Expand Down Expand Up @@ -45,24 +43,22 @@ func Load() error {
libName := C.CString(mxsmlLibName)
defer C.free(unsafe.Pointer(libName))

runtime.LockOSThread()
handle := C.dlopen(libName, C.int(g_mxsmlLib.flag))
runtime.UnlockOSThread()
var handle unsafe.Pointer
if !hasExplicitLibraryPathConfig() {
runtime.LockOSThread()
handle = C.dlopen(libName, C.int(g_mxsmlLib.flag))
runtime.UnlockOSThread()

if handle != nil {
g_mxsmlLib.handle = handle
g_mxsmlLib.loaded = true
return nil
if handle != nil {
g_mxsmlLib.handle = handle
g_mxsmlLib.loaded = true
return nil
}
}

var installPath string
// check lib path
libPathList := []string{
"/opt/mxdriver/lib/" + mxsmlLibName,
"/opt/maca/lib/" + mxsmlLibName,
"/opt/mxn100/lib/" + mxsmlLibName,
}
for _, path := range libPathList {
attemptedPaths := candidateLibraryPaths()
for _, path := range attemptedPaths {
if _, err := os.Stat(path); err == nil {
installPath = path
break
Expand Down
75 changes: 75 additions & 0 deletions pkg/lib/lib_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package lib

import (
"os"
"path/filepath"
"testing"
)

func TestCandidateLibraryPathsUsesEnvFile(t *testing.T) {
t.Setenv("MXSML_LIBRARY_PATH", "/custom/libmxsml.so")
t.Setenv("MACA_HOME", "")

paths := candidateLibraryPaths()
if len(paths) != 1 || paths[0] != "/custom/libmxsml.so" {
t.Fatalf("expected env file path first, got %v", paths)
}
}

func TestCandidateLibraryPathsUsesEnvDirectory(t *testing.T) {
dir := t.TempDir()
t.Setenv("MXSML_LIBRARY_PATH", dir)
t.Setenv("MACA_HOME", "")

paths := candidateLibraryPaths()
want := filepath.Join(dir, mxsmlLibName)
if len(paths) != 1 || paths[0] != want {
t.Fatalf("expected env directory to resolve to %s, got %v", want, paths)
}
}

func TestCandidateLibraryPathsUsesMacaHome(t *testing.T) {
t.Setenv("MXSML_LIBRARY_PATH", "")
t.Setenv("MACA_HOME", "/opt/custom-maca")

paths := candidateLibraryPaths()
want := filepath.Join("/opt/custom-maca", "lib", mxsmlLibName)
if len(paths) != 1 || paths[0] != want {
t.Fatalf("expected only explicit MACA_HOME path %s, got %v", want, paths)
}
}

func TestCandidateLibraryPathsSkipsDuplicates(t *testing.T) {
t.Setenv("MXSML_LIBRARY_PATH", filepath.Join("/opt/maca/lib", mxsmlLibName))
t.Setenv("MACA_HOME", "/opt/maca")

paths := candidateLibraryPaths()
seen := map[string]struct{}{}
for _, path := range paths {
if _, ok := seen[path]; ok {
t.Fatalf("found duplicate path %s in %v", path, paths)
}
seen[path] = struct{}{}
}
}

func TestCandidateLibraryPathsHandlesMissingEnvDirAsFile(t *testing.T) {
missing := filepath.Join(os.TempDir(), "missing-libmxsml.so")
t.Setenv("MXSML_LIBRARY_PATH", missing)
t.Setenv("MACA_HOME", "")

paths := candidateLibraryPaths()
if len(paths) == 0 || paths[0] != missing {
t.Fatalf("expected missing env path to be preserved, got %v", paths)
}
}

func TestCandidateLibraryPathsFallsBackToDefaultsWithoutExplicitConfig(t *testing.T) {
t.Setenv("MXSML_LIBRARY_PATH", "")
t.Setenv("MACA_HOME", "")

paths := candidateLibraryPaths()
if len(paths) != 3 {
t.Fatalf("expected default paths, got %v", paths)
}
}
51 changes: 51 additions & 0 deletions pkg/lib/search_paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package lib

import (
"os"
"path/filepath"
"strings"
)

const mxsmlLibName = "libmxsml.so"

func hasExplicitLibraryPathConfig() bool {
return strings.TrimSpace(os.Getenv("MXSML_LIBRARY_PATH")) != "" ||
strings.TrimSpace(os.Getenv("MACA_HOME")) != ""
}

func candidateLibraryPaths() []string {
paths := []string{}
appendUnique := func(path string) {
if path == "" {
return
}
for _, existing := range paths {
if existing == path {
return
}
}
paths = append(paths, path)
}

if value := strings.TrimSpace(os.Getenv("MXSML_LIBRARY_PATH")); value != "" {
info, err := os.Stat(value)
if err == nil && info.IsDir() {
appendUnique(filepath.Join(value, mxsmlLibName))
} else {
appendUnique(value)
}
}

if value := strings.TrimSpace(os.Getenv("MACA_HOME")); value != "" {
appendUnique(filepath.Join(value, "lib", mxsmlLibName))
}

if hasExplicitLibraryPathConfig() {
return paths
}

appendUnique("/opt/mxdriver/lib/" + mxsmlLibName)
appendUnique("/opt/maca/lib/" + mxsmlLibName)
appendUnique("/opt/mxn100/lib/" + mxsmlLibName)
return paths
}