Skip to content

Commit

Permalink
Merge pull request #401 from skycoin/ewintory/cmd
Browse files Browse the repository at this point in the history
Move cmd related code to separate package
  • Loading branch information
SkycoinSynth committed Feb 28, 2021
2 parents d6cbf84 + 024143c commit e209fe2
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 84 deletions.
11 changes: 6 additions & 5 deletions Makefile
Expand Up @@ -65,19 +65,19 @@ ifeq ($(UNAME_S), Linux)
endif

build: ## Build CX from sources
go build $(GO_OPTS) -tags="base" -i -o ./bin/cx github.com/skycoin/cx/cxgo/
go build $(GO_OPTS) -tags="base" -i -o ./bin/cx github.com/skycoin/cx/cmd/cx/
chmod +x ./bin/cx

clean: ## Removes binaries.
rm -r ./bin/cx

build-full: install-full ## Build CX from sources with all build tags
go build $(GO_OPTS) -tags="base cxfx" -i -o ./bin/cx github.com/skycoin/cx/cxgo/
go build $(GO_OPTS) -tags="base cxfx" -i -o ./bin/cx github.com/skycoin/cx/cmd/cx/
chmod +x ./bin/cx

build-android: install-full install-mobile
# TODO @evanlinjin: We should switch this to use 'github.com/SkycoinProject/gomobile' once it can build.
go get $(GO_OPTS) -u golang.org/x/mobile/cmd/gomobile
go get $(GO_OPTS) -u golang.org/x/mobile/cmd/cx/cmd/mobile/

token-fuzzer:
go build $(GO_OPTS) -i -o ./bin/cx-token-fuzzer $(PWD)/development/token-fuzzer/main.go
Expand All @@ -101,19 +101,20 @@ test: ## Run CX test suite.
ifndef CXVERSION
@echo "cx not found in $(PWD)/bin, please run make install first"
else
go test $(GO_OPTS) -race -tags base github.com/skycoin/cx/cxgo/
go test $(GO_OPTS) -race -tags base github.com/skycoin/cx/cmd/cx
./bin/cx ./lib/args.cx ./tests/main.cx ++wdir=./tests ++disable-tests=gui,issue
endif

test-full: build ## Run CX test suite with all build tags
go test $(GO_OPTS) -race -tags="base cxfx" github.com/skycoin/cx/cxgo/
go test $(GO_OPTS) -race -tags="base cxfx" github.com/skycoin/cx/cmd/cx
./bin/cx ./lib/args.cx ./tests/main.cx ++wdir=./tests ++disable-tests=gui,issue

configure-workspace: ## Configure CX workspace environment
mkdir -p $(CX_PATH)/src $(CX_PATH)/bin $(CX_PATH)/pkg
@echo "NOTE:\tCX workspace at $(CX_PATH)"

format: ## Formats the code. Must have goimports installed (use make install-linters).
goimports -w -local github.com/skycoin/cx ./cmd
goimports -w -local github.com/skycoin/cx ./cx
goimports -w -local github.com/skycoin/cx ./cxfx
goimports -w -local github.com/skycoin/cx ./cxgo
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
154 changes: 76 additions & 78 deletions cxgo/main.go → cmd/cx/main.go
Expand Up @@ -25,6 +25,82 @@ func main() {
}
}

func Run(args []string) {
runtime.LockOSThread()
runtime.GOMAXPROCS(2)

options := defaultCmdFlags()
parseFlags(&options, args)

// Checking if CXPATH is set, either by setting an environment variable
// or by setting the `--cxpath` flag.
checkCXPathSet(options)

if checkHelp(args) {
commandLine.PrintDefaults()
return
}

// Does the user want to print the command-line help?
if options.printHelp {
printHelp()
return
}

// Does the user want to print CX's version?
if options.printVersion {
printVersion()
return
}

// User wants to print CX env
if options.printEnv {
printEnv()
return
}

if options.initialHeap != "" {
cxcore.INIT_HEAP_SIZE = parseMemoryString(options.initialHeap)
}
if options.maxHeap != "" {
cxcore.MAX_HEAP_SIZE = parseMemoryString(options.maxHeap)
if cxcore.MAX_HEAP_SIZE < cxcore.INIT_HEAP_SIZE {
// Then MAX_HEAP_SIZE overrides INIT_HEAP_SIZE's value.
cxcore.INIT_HEAP_SIZE = cxcore.MAX_HEAP_SIZE
}
}
if options.stackSize != "" {
cxcore.STACK_SIZE = parseMemoryString(options.stackSize)
actions.DataOffset = cxcore.STACK_SIZE
}
if options.minHeapFreeRatio != float64(0) {
cxcore.MIN_HEAP_FREE_RATIO = float32(options.minHeapFreeRatio)
}
if options.maxHeapFreeRatio != float64(0) {
cxcore.MAX_HEAP_FREE_RATIO = float32(options.maxHeapFreeRatio)
}

// options, file pointers, filenames
cxArgs, sourceCode, fileNames := cxcore.ParseArgsForCX(commandLine.Args(), true)

// Propagate some options out to other packages.
parser.DebugLexer = options.debugLexer // in package parser
DebugProfileRate = options.debugProfile
DebugProfile = DebugProfileRate > 0

if run, bcHeap, sPrgrm := parseProgram(options, fileNames, sourceCode); run {
runProgram(options, cxArgs, sourceCode, bcHeap, sPrgrm)
}
}

// initMainPkg adds a `main` package with an empty `main` function to `prgrm`.
func initMainPkg(prgrm *cxcore.CXProgram) {
mod := cxcore.MakePackage(cxcore.MAIN_PKG)
prgrm.AddPackage(mod)
fn := cxcore.MakeFunction(cxcore.MAIN_FUNC, actions.CurrentFile, actions.LineNo)
mod.AddFunction(fn)
}

// optionTokenize checks if the user wants to use CX to generate the lexer tokens
func optionTokenize(options cxCmdFlags, fileNames []string) {
var r *os.File
Expand Down Expand Up @@ -61,14 +137,6 @@ func optionTokenize(options cxCmdFlags, fileNames []string) {
parser.Tokenize(r, w)
}

// initMainPkg adds a `main` package with an empty `main` function to `prgrm`.
func initMainPkg(prgrm *cxcore.CXProgram) {
mod := cxcore.MakePackage(cxcore.MAIN_PKG)
prgrm.AddPackage(mod)
fn := cxcore.MakeFunction(cxcore.MAIN_FUNC, actions.CurrentFile, actions.LineNo)
mod.AddFunction(fn)
}

func parseProgram(options cxCmdFlags, fileNames []string, sourceCode []*os.File) (bool, []byte, []byte) {
profile := StartCPUProfile("parse")
defer StopCPUProfile(profile)
Expand Down Expand Up @@ -160,76 +228,6 @@ func runProgram(options cxCmdFlags, cxArgs []string, sourceCode []*os.File, bcHe
if cxcore.AssertFailed() {
os.Exit(cxcore.CX_ASSERT)
}

}

func Run(args []string) {
runtime.LockOSThread()
runtime.GOMAXPROCS(2)

options := defaultCmdFlags()
parseFlags(&options, args)

// Checking if CXPATH is set, either by setting an environment variable
// or by setting the `--cxpath` flag.
checkCXPathSet(options)

if checkHelp(args) {
commandLine.PrintDefaults()
return

}

// Does the user want to print the command-line help?
if options.printHelp {
printHelp()
return
}

// Does the user want to print CX's version?
if options.printVersion {
printVersion()
return
}

// User wants to print CX env
if options.printEnv {
printEnv()
return
}

if options.initialHeap != "" {
cxcore.INIT_HEAP_SIZE = parseMemoryString(options.initialHeap)
}
if options.maxHeap != "" {
cxcore.MAX_HEAP_SIZE = parseMemoryString(options.maxHeap)
if cxcore.MAX_HEAP_SIZE < cxcore.INIT_HEAP_SIZE {
// Then MAX_HEAP_SIZE overrides INIT_HEAP_SIZE's value.
cxcore.INIT_HEAP_SIZE = cxcore.MAX_HEAP_SIZE
}
}
if options.stackSize != "" {
cxcore.STACK_SIZE = parseMemoryString(options.stackSize)
actions.DataOffset = cxcore.STACK_SIZE
}
if options.minHeapFreeRatio != float64(0) {
cxcore.MIN_HEAP_FREE_RATIO = float32(options.minHeapFreeRatio)
}
if options.maxHeapFreeRatio != float64(0) {
cxcore.MAX_HEAP_FREE_RATIO = float32(options.maxHeapFreeRatio)
}

// options, file pointers, filenames
cxArgs, sourceCode, fileNames := cxcore.ParseArgsForCX(commandLine.Args(), true)

// Propagate some options out to other packages.
parser.DebugLexer = options.debugLexer // in package parser
DebugProfileRate = options.debugProfile
DebugProfile = DebugProfileRate > 0

if run, bcHeap, sPrgrm := parseProgram(options, fileNames, sourceCode); run {
runProgram(options, cxArgs, sourceCode, bcHeap, sPrgrm)
}
}

// Used for the -heap-initial, -heap-max and -stack-size flags.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion cxgo/cxgo/cxgo.go
Expand Up @@ -5,9 +5,9 @@ import (
"bytes"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"path/filepath"

cxcore "github.com/skycoin/cx/cx"
"github.com/skycoin/cx/cxgo/actions"
Expand Down

0 comments on commit e209fe2

Please sign in to comment.