Skip to content

Commit

Permalink
feat: disable shared lib install at the code level (#259)
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy authored Mar 19, 2024
1 parent cf82a2e commit d3ed5cb
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 26 deletions.
91 changes: 91 additions & 0 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package env

import (
"os"
"strconv"
"sync"
)

var (
once sync.Once
mu sync.Mutex
env *EnvSettings
)

// EnvSettings represents environment settings for the KCL Go SDK.
type EnvSettings struct {
LibHome string
DisableInstallArtifact bool
DisableUseArtifactInPath bool
}

// Instance returns a singleton instance of EnvSettings.
func instance() *EnvSettings {
once.Do(func() {
env = &EnvSettings{
LibHome: os.Getenv("KCL_LIB_HOME"),
DisableInstallArtifact: envBoolOr("KCL_GO_DISABLE_INSTALL_ARTIFACT", false),
DisableUseArtifactInPath: envBoolOr("KCL_GO_DISABLE_ARTIFACT_IN_PATH", false),
}
})
return env
}

// GetLibHome returns the LibHome value from the singleton instance of EnvSettings.
func GetLibHome() string {
return instance().LibHome
}

// SetLibHome sets the LibHome value in the singleton instance of EnvSettings.
func SetLibHome(value string) {
mu.Lock()
defer mu.Unlock()
instance().LibHome = value
}

// SetDisableInstallArtifact sets the DisableInstallArtifact value in the singleton instance of EnvSettings.
func SetDisableInstallArtifact(value bool) {
mu.Lock()
defer mu.Unlock()
instance().DisableInstallArtifact = value
}

// GetDisableInstallArtifact returns the DisableInstallArtifact value from the singleton instance of EnvSettings.
func GetDisableInstallArtifact() bool {
return instance().DisableInstallArtifact
}

// GetDisableUseArtifactInPath returns the DisableUseArtifactInPath value from the singleton instance of EnvSettings.
func GetDisableUseArtifactInPath() bool {
return instance().DisableUseArtifactInPath
}

// SetDisableUseArtifactInPath sets the DisableUseArtifactInPath value in the singleton instance of EnvSettings.
func SetDisableUseArtifactInPath(value bool) {
mu.Lock()
defer mu.Unlock()
instance().DisableUseArtifactInPath = value
}

// envOr returns the value of the specified environment variable, or the
// default value if it does not exist.
func envOr(name, def string) string {
if v, ok := os.LookupEnv(name); ok {
return v
}
return def
}

// envBoolOr returns the boolean value of the specified environment variable,
// or the default value if it does not exist.
func envBoolOr(name string, def bool) bool {
if name == "" {
return def
}
envVal := envOr(name, strconv.FormatBool(def))
ret, err := strconv.ParseBool(envVal)
if err != nil {
return def
}
return ret
}
5 changes: 5 additions & 0 deletions pkg/native/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"kcl-lang.io/kcl-go/pkg/3rdparty/dlopen"
kcl_runtime "kcl-lang.io/kcl-go/pkg/runtime"
"kcl-lang.io/kcl-go/pkg/utils"
)

const libName = "kclvm_cli_cdylib"
Expand All @@ -24,6 +25,10 @@ func loadServiceNativeLib() *dlopen.LibHandle {
}

libPath := filepath.Join(root, "bin", fullLibName)
if !utils.FileExists(libPath) {
libPath = filepath.Join(root, "lib", fullLibName)
}

libPaths = append(libPaths, libPath)

h, err := dlopen.GetHandle(libPaths)
Expand Down
8 changes: 2 additions & 6 deletions pkg/path/lazypath.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@ package path
import (
"os"
"path/filepath"
)

const (
// LibHomeEnvVar is the environment variable used by kcl
// for the lib install directory. When no value is set a default is used.
LibHomeEnvVar = "KCL_LIB_HOME"
"kcl-lang.io/kcl-go/pkg/env"
)

// lazypath is an lazy-loaded path buffer for the XDG base directory specification.
Expand All @@ -45,5 +41,5 @@ func (l lazypath) path(envVar string, defaultFn func() string, elem ...string) s
// libPath defines the base directory relative to which user specific non-essential data files
// should be stored.
func (l lazypath) libPath(elem ...string) string {
return l.path(LibHomeEnvVar, libHome, filepath.Join(elem...))
return l.path(env.GetLibHome(), libHome, filepath.Join(elem...))
}
18 changes: 4 additions & 14 deletions pkg/runtime/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ package runtime

import (
"fmt"
"os"
"path/filepath"
"runtime"
"sync"

"kcl-lang.io/kcl-go/pkg/spec/gpyrpc"
)

var (
Debug bool
rpcRuntime *Runtime
once sync.Once
UseKCLPluginEnvVar = "KCL_GO_USE_PLUGIN"
Debug bool
rpcRuntime *Runtime
once sync.Once
)

const tip = "Tip: Have you used a binary version of KCL in your PATH that is not consistent with the KCL Go SDK? You can upgrade or reduce the KCL version or delete the KCL in your PATH"
Expand Down Expand Up @@ -47,14 +44,7 @@ func initRuntime(maxProc int) {
panic(ErrKclvmRootNotFound)
}

if os.Getenv(UseKCLPluginEnvVar) != "" {
os.Setenv("PYTHONHOME", "")
os.Setenv("PYTHONPATH", filepath.Join(g_KclvmRoot, "lib", "site-packages"))
rpcRuntime = NewRuntime(int(maxProc), MustGetKclvmPath(), "-m", "kclvm.program.rpc-server")
} else {
rpcRuntime = NewRuntime(int(maxProc), "kclvm_cli", "server")
}

rpcRuntime = NewRuntime(int(maxProc), "kclvm_cli", "server")
rpcRuntime.Start()

client := &BuiltinServiceClient{
Expand Down
20 changes: 14 additions & 6 deletions pkg/runtime/kclvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@ import (
"time"

"github.com/gofrs/flock"
"kcl-lang.io/kcl-go/pkg/env"
"kcl-lang.io/kcl-go/pkg/logger"
"kcl-lang.io/kcl-go/pkg/path"
"kcl-lang.io/lib"
)

const (
DisableArtifactEnvVar = "KCL_GO_DISABLE_ARTIFACT"
)

func init() {
if os.Getenv(DisableArtifactEnvVar) == "" && os.Getenv(UseKCLPluginEnvVar) == "" {
if !env.GetDisableInstallArtifact() {
installKclArtifact()
}
g_KclvmRoot = findKclvmRoot()
if env.GetDisableUseArtifactInPath() {
// Must use the artifact installed by kcl-go
// Get the install lib path.
g_KclvmRoot = findInstalledArtifactRoot()
} else {
// Get kclvm root path in PATH
g_KclvmRoot = findKclvmRoot()
}
}

func installKclArtifact() {
Expand Down Expand Up @@ -112,3 +116,7 @@ func findKclvmRoot() string {
}
return ""
}

func findInstalledArtifactRoot() string {
return filepath.Join(path.LibPath(), "bin")
}
18 changes: 18 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
// Copyright The KCL Authors. All rights reserved.

package utils

import "os"

func FileExists(path string) bool {
fi, err := os.Lstat(path)
if err != nil || fi.IsDir() {
return false
}
return true
}

func DirExists(path string) bool {
fi, err := os.Lstat(path)
if err != nil || !fi.IsDir() {
return false
}
return true
}

0 comments on commit d3ed5cb

Please sign in to comment.