-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from Anthony-Dong/dev
Dev
- Loading branch information
Showing
85 changed files
with
2,946 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package cpp | ||
|
||
import ( | ||
"encoding/json" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/anthony-dong/golang/pkg/utils" | ||
) | ||
|
||
func ReadToolConfigFromFile(fileName string, tool *Tools) error { | ||
kv := make(map[string]interface{}) | ||
if err := utils.UnmarshalFromFile(fileName, kv); err != nil { | ||
return err | ||
} | ||
return ReadToolConfigFromKV(kv, runtime.GOOS, tool) | ||
} | ||
|
||
func ReadToolConfigFromKV(kv map[string]interface{}, os string, tool *Tools) error { | ||
osKv := make(map[string]interface{}, len(kv)) | ||
for key, value := range kv { | ||
keys := strings.SplitN(key, "@", 2) | ||
if len(keys) != 2 { | ||
continue | ||
} | ||
if keys[1] == os { | ||
osKv[keys[0]] = value | ||
} | ||
} | ||
for k, v := range osKv { | ||
kv[k] = v | ||
} | ||
marshal, err := json.Marshal(kv) | ||
if err != nil { | ||
return err | ||
} | ||
if err := json.Unmarshal(marshal, tool); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cpp | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReadToolConfigFromFile(t *testing.T) { | ||
tools := &Tools{} | ||
err := ReadToolConfigFromKV(map[string]interface{}{ | ||
"CXX": "/opt/homebrew/opt/llvm@14/bin/clang++", | ||
"CC": "/opt/homebrew/opt/llvm@14/bin/clang++", | ||
"CXX@linux": "/usr/lib/llvm-14/bin/clang++", | ||
"CC@linux": "/usr/lib/llvm-14/bin/clang", | ||
}, "linux", tools) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
assert.Equal(t, tools.CXX, "/usr/lib/llvm-14/bin/clang++") | ||
assert.Equal(t, tools.CC, "/usr/lib/llvm-14/bin/clang") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package cpp | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/anthony-dong/golang/pkg/utils" | ||
) | ||
|
||
type buildAndLink struct { | ||
buildArgs []string | ||
linkArgs []string | ||
} | ||
|
||
var buildArgsRegexp = regexp.MustCompile(`^//\s*(build|cxxopt)\s*:\s*`) | ||
var linkArgsRegexp = regexp.MustCompile(`^//\s*(link|linkopt)\s*:\s*`) | ||
|
||
func readFileArgs(file string) (*buildAndLink, error) { | ||
open, err := os.Open(file) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer open.Close() | ||
return readBuildAndLinkArgs(open) | ||
} | ||
|
||
func readBuildAndLinkArgs(reader io.Reader) (*buildAndLink, error) { | ||
result := &buildAndLink{} | ||
utils.ReadSomeLines(reader, func(index int, line string) bool { | ||
if strings.HasPrefix(line, "#include") { | ||
return false | ||
} | ||
if prefix := linkArgsRegexp.FindString(line); prefix != "" { | ||
args := strings.TrimPrefix(line, prefix) | ||
result.linkArgs = append(result.linkArgs, utils.SplitArgs(args)...) | ||
} | ||
if prefix := buildArgsRegexp.FindString(line); prefix != "" { | ||
args := strings.TrimPrefix(line, prefix) | ||
result.buildArgs = append(result.buildArgs, utils.SplitArgs(args)...) | ||
} | ||
return true | ||
}) | ||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package cpp | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_readFileArgs(t *testing.T) { | ||
args, err := readBuildAndLinkArgs(bytes.NewBufferString(` | ||
// link: -lspdlog | ||
// build: -O2 | ||
// cxxopt: -g | ||
// link: -L/usr/local/lib | ||
// linkopt: -lgtest | ||
`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Logf("%#v\n", args) | ||
assert.Equal(t, args, &buildAndLink{buildArgs: []string{"-O2", "-g"}, linkArgs: []string{"-lspdlog", "-L/usr/local/lib", "-lgtest"}}) | ||
} |
Oops, something went wrong.