build: separate compiler and libs

This commit is contained in:
Li Jie
2025-01-07 21:49:08 +08:00
parent b0123567cd
commit 1172e5bdce
559 changed files with 190 additions and 176 deletions

View File

@@ -1,114 +0,0 @@
/*
* Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package base defines shared basic pieces of the llgo command,
// in particular logging and the Command structure.
package base
import (
"flag"
"fmt"
"io"
"os"
"strings"
)
// A Command is an implementation of a gop command
// like gop export or gop install.
type Command struct {
// Run runs the command.
// The args are the arguments after the command name.
Run func(cmd *Command, args []string)
// UsageLine is the one-line usage message.
// The words between "gop" and the first flag or argument in the line are taken to be the command name.
UsageLine string
// Short is the short description shown in the 'gop help' output.
Short string
// Flag is a set of flags specific to this command.
Flag flag.FlagSet
// Commands lists the available commands and help topics.
// The order here is the order in which they are printed by 'gop help'.
// Note that subcommands are in general best avoided.
Commands []*Command
}
// Llgo command
var Llgo = &Command{
UsageLine: "llgo",
Short: `llgo is a Go compiler based on LLVM in order to better integrate Go with the C ecosystem including Python.`,
// Commands initialized in package main
}
// LongName returns the command's long name: all the words in the usage line between "gop" and a flag or argument,
func (c *Command) LongName() string {
name := c.UsageLine
if i := strings.Index(name, " ["); i >= 0 {
name = name[:i]
}
if name == "llgo" {
return ""
}
return strings.TrimPrefix(name, "llgo ")
}
// Name returns the command's short name: the last word in the usage line before a flag or argument.
func (c *Command) Name() string {
name := c.LongName()
if i := strings.LastIndex(name, " "); i >= 0 {
name = name[i+1:]
}
return name
}
// Usage show the command usage.
func (c *Command) Usage(w io.Writer) {
fmt.Fprintf(w, "%s\n\nUsage: %s\n", c.Short, c.UsageLine)
// restore output of flag
defer c.Flag.SetOutput(c.Flag.Output())
c.Flag.SetOutput(w)
c.Flag.PrintDefaults()
fmt.Fprintln(w)
os.Exit(2)
}
// Runnable reports whether the command can be run; otherwise
// it is a documentation pseudo-command.
func (c *Command) Runnable() bool {
return c.Run != nil
}
// Usage is the usage-reporting function, filled in by package main
// but here for reference by other packages.
//
// flag.Usage func()
// CmdName - "build", "install", "list", "mod tidy", etc.
var CmdName string
// Main runs a command.
func Main(c *Command, app string, args []string) {
name := c.UsageLine
if i := strings.Index(name, " ["); i >= 0 {
c.UsageLine = app + name[i:]
}
c.Run(c, args)
}

View File

@@ -1,52 +0,0 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package build implements the "llgo build" command.
package build
import (
"fmt"
"os"
"github.com/goplus/llgo/cmd/internal/base"
"github.com/goplus/llgo/internal/build"
)
// llgo build
var Cmd = &base.Command{
UsageLine: "llgo build [-o output] [build flags] [packages]",
Short: "Compile packages and dependencies",
}
func init() {
Cmd.Run = runCmd
}
func runCmd(cmd *base.Command, args []string) {
conf := &build.Config{
Mode: build.ModeBuild,
AppExt: build.DefaultAppExt(),
}
if len(args) >= 2 && args[0] == "-o" {
conf.OutFile = args[1]
args = args[2:]
}
_, err := build.Do(args, conf)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

View File

@@ -1,38 +0,0 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package clean implements the "llgo clean" command.
package clean
import (
"github.com/goplus/llgo/cmd/internal/base"
"github.com/goplus/llgo/internal/build"
)
// llgo build
var Cmd = &base.Command{
UsageLine: "llgo clean [clean flags] [build flags] [packages]",
Short: "Remove object files and cached files",
}
func init() {
Cmd.Run = runCmd
}
func runCmd(cmd *base.Command, args []string) {
conf := build.NewDefaultConf(0)
build.Clean(args, conf)
}

View File

@@ -1,36 +0,0 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package get implements the "llgo get" command.
package get
import (
"github.com/goplus/llgo/cmd/internal/base"
)
// llgo get
var Cmd = &base.Command{
UsageLine: "llgo get [-t -u -v] [build flags] [packages]",
Short: "Add dependencies to current module and install them",
}
func init() {
Cmd.Run = runCmd
}
func runCmd(cmd *base.Command, args []string) {
panic("todo")
}

View File

@@ -1,123 +0,0 @@
/*
* Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package help implements the “llgo help” command.
package help
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strings"
"text/template"
"unicode"
"unicode/utf8"
"github.com/goplus/llgo/cmd/internal/base"
)
// Help implements the 'help' command.
func Help(w io.Writer, args []string) {
cmd := base.Llgo
Args:
for i, arg := range args {
for _, sub := range cmd.Commands {
if sub.Name() == arg {
cmd = sub
continue Args
}
}
// helpSuccess is the help command using as many args as possible that would succeed.
helpSuccess := "llgo help"
if i > 0 {
helpSuccess += " " + strings.Join(args[:i], " ")
}
fmt.Fprintf(os.Stderr, "llgo help %s: unknown help topic. Run '%s'.\n", strings.Join(args, " "), helpSuccess)
os.Exit(2)
}
if len(cmd.Commands) > 0 {
PrintUsage(w, cmd)
} else {
cmd.Usage(w)
}
// not exit 2: succeeded at 'llgo help cmd'.
}
var usageTemplate = `{{.Short | trim}}
Usage:
{{.UsageLine}} <command> [arguments]
The commands are:
{{range .Commands}}{{if or (.Runnable) .Commands}}
{{.Name | printf "%-11s"}} {{.Short}}{{end}}{{end}}
Use "llgo help{{with .LongName}} {{.}}{{end}} <command>" for more information about a command.
`
// An errWriter wraps a writer, recording whether a write error occurred.
type errWriter struct {
w io.Writer
err error
}
func (w *errWriter) Write(b []byte) (int, error) {
n, err := w.w.Write(b)
if err != nil {
w.err = err
}
return n, err
}
// tmpl executes the given template text on data, writing the result to w.
func tmpl(w io.Writer, text string, data interface{}) {
t := template.New("top")
t.Funcs(template.FuncMap{"trim": strings.TrimSpace, "capitalize": capitalize})
template.Must(t.Parse(text))
ew := &errWriter{w: w}
err := t.Execute(ew, data)
if ew.err != nil {
// I/O error writing. Ignore write on closed pipe.
if strings.Contains(ew.err.Error(), "pipe") {
os.Exit(1)
}
log.Fatalf("writing output: %v", ew.err)
}
if err != nil {
panic(err)
}
}
func capitalize(s string) string {
if s == "" {
return s
}
r, n := utf8.DecodeRuneInString(s)
return string(unicode.ToTitle(r)) + s[n:]
}
// PrintUsage prints usage information.
func PrintUsage(w io.Writer, cmd *base.Command) {
bw := bufio.NewWriter(w)
tmpl(bw, usageTemplate, cmd)
bw.Flush()
}

View File

@@ -1,45 +0,0 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package install implements the "llgo install" command.
package install
import (
"fmt"
"os"
"github.com/goplus/llgo/cmd/internal/base"
"github.com/goplus/llgo/internal/build"
)
// llgo install
var Cmd = &base.Command{
UsageLine: "llgo install [build flags] [packages]",
Short: "Compile and install packages and dependencies",
}
func init() {
Cmd.Run = runCmd
}
func runCmd(cmd *base.Command, args []string) {
conf := build.NewDefaultConf(build.ModeInstall)
_, err := build.Do(args, conf)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

View File

@@ -1,100 +0,0 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Package run implements the "llgo run" command.
package run
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/goplus/llgo/cmd/internal/base"
"github.com/goplus/llgo/internal/build"
)
var (
errNoProj = errors.New("llgo: no go files listed")
)
// llgo run
var Cmd = &base.Command{
UsageLine: "llgo run [build flags] package [arguments...]",
Short: "Compile and run Go program",
}
// llgo cmptest
var CmpTestCmd = &base.Command{
UsageLine: "llgo cmptest [-gen] [build flags] package [arguments...]",
Short: "Compile and run with llgo, compare result (stdout/stderr/exitcode) with go or llgo.expect; generate llgo.expect file if -gen is specified",
}
func init() {
Cmd.Run = runCmd
CmpTestCmd.Run = runCmpTest
}
func runCmd(cmd *base.Command, args []string) {
runCmdEx(cmd, args, build.ModeRun)
}
func runCmpTest(cmd *base.Command, args []string) {
runCmdEx(cmd, args, build.ModeCmpTest)
}
func runCmdEx(_ *base.Command, args []string, mode build.Mode) {
conf := build.NewDefaultConf(mode)
if mode == build.ModeCmpTest && len(args) > 0 && args[0] == "-gen" {
conf.GenExpect = true
args = args[1:]
}
args, runArgs, err := parseRunArgs(args)
check(err)
conf.RunArgs = runArgs
_, err = build.Do(args, conf)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func parseRunArgs(args []string) ([]string, []string, error) {
n := build.SkipFlagArgs(args)
if n < 0 {
return nil, nil, errNoProj
}
arg := args[n]
if isGoFile(arg) {
n++
for n < len(args) && isGoFile(args[n]) {
n++
}
return args[:n], args[n:], nil
}
return args[:n+1], args[n+1:], nil
}
func isGoFile(fname string) bool {
return filepath.Ext(fname) == ".go"
}
func check(err error) {
if err != nil {
panic(err)
}
}

View File

@@ -1,39 +0,0 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
// Package version implements the "llgo version" command.
package version
import (
"fmt"
"runtime"
"github.com/goplus/llgo/cmd/internal/base"
"github.com/goplus/llgo/x/env"
)
// llgo version
var Cmd = &base.Command{
UsageLine: "llgo version",
Short: "Print LLGo version",
}
func init() {
Cmd.Run = runCmd
}
func runCmd(cmd *base.Command, args []string) {
fmt.Printf("llgo %s %s/%s\n", env.Version(), runtime.GOOS, runtime.GOARCH)
}

View File

@@ -1,102 +0,0 @@
/*
* Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/qiniu/x/log"
"github.com/goplus/llgo/cmd/internal/base"
"github.com/goplus/llgo/cmd/internal/build"
"github.com/goplus/llgo/cmd/internal/clean"
"github.com/goplus/llgo/cmd/internal/get"
"github.com/goplus/llgo/cmd/internal/help"
"github.com/goplus/llgo/cmd/internal/install"
"github.com/goplus/llgo/cmd/internal/run"
"github.com/goplus/llgo/cmd/internal/version"
)
func mainUsage() {
help.PrintUsage(os.Stderr, base.Llgo)
os.Exit(2)
}
func init() {
flag.Usage = mainUsage
base.Llgo.Commands = []*base.Command{
build.Cmd,
install.Cmd,
get.Cmd,
run.Cmd,
run.CmpTestCmd,
clean.Cmd,
version.Cmd,
}
}
func main() {
flag.Parse()
args := flag.Args()
if len(args) < 1 {
flag.Usage()
}
log.SetFlags(log.Ldefault &^ log.LstdFlags)
base.CmdName = args[0] // for error messages
if args[0] == "help" {
help.Help(os.Stderr, args[1:])
return
}
BigCmdLoop:
for bigCmd := base.Llgo; ; {
for _, cmd := range bigCmd.Commands {
if cmd.Name() != args[0] {
continue
}
args = args[1:]
if len(cmd.Commands) > 0 {
bigCmd = cmd
if len(args) == 0 {
help.PrintUsage(os.Stderr, bigCmd)
os.Exit(2)
}
if args[0] == "help" {
help.Help(os.Stderr, append(strings.Split(base.CmdName, " "), args[1:]...))
return
}
base.CmdName += " " + args[0]
continue BigCmdLoop
}
if !cmd.Runnable() {
continue
}
cmd.Run(cmd, args)
return
}
helpArg := ""
if i := strings.LastIndex(base.CmdName, " "); i >= 0 {
helpArg = " " + base.CmdName[:i]
}
fmt.Fprintf(os.Stderr, "llgo %s: unknown command\nRun 'llgo help%s' for usage.\n", base.CmdName, helpArg)
os.Exit(2)
}
}