Skip to content

Commit

Permalink
Added support for pkgconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
codecat committed Jul 13, 2020
1 parent ffcaf40 commit bde8962
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ Adds a package to link to by its name. `qb` will try to resolve the package by i
]
defines = [ "SFML_STATIC" ]
```
2. Nothing else yet, but the following sources are planned: global configuration (like local, but system-wide), pkgconfig (for Linux and Mac), and vcpkg (for Windows).
2. **pkgconfig**: If you have `pkg-config` installed on your system, it will be checking for packages from there.
3. Nothing else yet, but the following is planned: global configuration (like local, but system-wide), and vcpkg (for Windows).

For example, to link with SFML, we can add `--pkg sfml`, as long as `sfml` can be resolved by one of the package sources.

Expand Down
35 changes: 35 additions & 0 deletions compiler_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,26 @@ func (ci darwinCompiler) Compile(path, objDir string, options *CompilerOptions)
args := make([]string, 0)
args = append(args, "-c")
args = append(args, "-o", filepath.Join(objDir, filename+".o"))

if options.Debug {
args = append(args, "-g")
}

// Add include directories
for _, dir := range options.IncludeDirectories {
args = append(args, "-I"+dir)
}

// Add precompiler definitions
for _, define := range options.Defines {
args = append(args, "-D"+define)
}

// Add additional compiler flags
for _, flag := range options.CompilerFlags {
args = append(args, flag)
}

args = append(args, path)

cmd := exec.Command("clang", args...)
Expand Down Expand Up @@ -70,6 +87,24 @@ func (ci darwinCompiler) Link(objDir, outPath string, outType LinkType, options
args = append(args, "-static")
log.Warn("Static linking is not supported on MacOS!")
}

// Add additional library paths
for _, dir := range options.LinkDirectories {
args = append(args, "-L"+dir)
}

// Link to some common standard libraries
args = append(args, "-lstdc++")

// Add libraries to link
for _, link := range options.LinkLibraries {
args = append(args, "-l"+link)
}

// Add additional linker flags
for _, flag := range options.LinkerFlags {
args = append(args, flag)
}
}

filepath.Walk(objDir, func(path string, info os.FileInfo, err error) error {
Expand Down
32 changes: 32 additions & 0 deletions compiler_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,26 @@ func (ci linuxCompiler) Compile(path, objDir string, options *CompilerOptions) e
args := make([]string, 0)
args = append(args, "-c")
args = append(args, "-o", filepath.Join(objDir, filename+".o"))

if options.Debug {
args = append(args, "-g")
}

// Add include directories
for _, dir := range options.IncludeDirectories {
args = append(args, "-I"+dir)
}

// Add precompiler definitions
for _, define := range options.Defines {
args = append(args, "-D"+define)
}

// Add additional compiler flags
for _, flag := range options.CompilerFlags {
args = append(args, flag)
}

args = append(args, path)

cmd := exec.Command("gcc", args...)
Expand Down Expand Up @@ -71,6 +88,21 @@ func (ci linuxCompiler) Link(objDir, outPath string, outType LinkType, options *
if options.Static {
args = append(args, "-static")
}

// Add additional library paths
for _, dir := range options.LinkDirectories {
args = append(args, "-L"+dir)
}

// Add libraries to link
for _, link := range options.LinkLibraries {
args = append(args, "-l"+link)
}

// Add additional linker flags
for _, flag := range options.LinkerFlags {
args = append(args, flag)
}
}

filepath.Walk(objDir, func(path string, info os.FileInfo, err error) error {
Expand Down
44 changes: 42 additions & 2 deletions package.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"os/exec"
"runtime"
"strings"

"github.com/mattn/go-shellwords"
"github.com/spf13/viper"
)

Expand All @@ -18,8 +21,10 @@ func addPackage(options *CompilerOptions, name string) *Package {

//TODO: Implement global packages

if runtime.GOOS == "linux" {
//TODO: Implement pkgconfig
if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
if ret := addPackagePkgconfig(options, name); ret != nil {
return ret
}
}

if runtime.GOOS == "windows" {
Expand All @@ -38,6 +43,41 @@ func addPackageLocal(options *CompilerOptions, name string) *Package {
return configurePackageFromConfig(options, packageInfo, name)
}

func addPackagePkgconfig(options *CompilerOptions, name string) *Package {
// pkg-config must be installed for this to work
_, err := exec.LookPath("pkg-config")
if err != nil {
return nil
}

cmdCflags := exec.Command("pkg-config", name, "--cflags")
outputCflags, err := cmdCflags.CombinedOutput()
if err != nil {
return nil
}

cmdLibs := exec.Command("pkg-config", name, "--libs")
outputLibs, err := cmdLibs.CombinedOutput()
if err != nil {
return nil
}

parseCflags, _ := shellwords.Parse(strings.Trim(string(outputCflags), "\r\n"))
parseLibs, _ := shellwords.Parse(strings.Trim(string(outputLibs), "\r\n"))

for _, flag := range parseCflags {
options.CompilerFlags = append(options.CompilerFlags, flag)
}

for _, flag := range parseLibs {
options.LinkerFlags = append(options.LinkerFlags, flag)
}

return &Package{
Name: name,
}
}

func configurePackageFromConfig(options *CompilerOptions, pkg map[string]interface{}, name string) *Package {
maybeUnpack(&options.IncludeDirectories, pkg["includes"])
maybeUnpack(&options.LinkDirectories, pkg["linkdirs"])
Expand Down

0 comments on commit bde8962

Please sign in to comment.