Skip to content

Commit

Permalink
Add build tags to support both x86 and ARM compilation on macOS (#1069)
Browse files Browse the repository at this point in the history
* Add build tags to support both x86 and ARM compilation on macOS

* Documentation fix: command line for build under macOS

* Global replace 'macOS X' -> 'macOS'. Fix typo in -tags cmd line param

* `README.md`: fix all `--tags` -> `-tags`

---------

Co-authored-by: Denis Dmitriev <dmitriev@itspartner.net>
  • Loading branch information
Spaider and Denis Dmitriev committed Feb 23, 2023
1 parent 819cc0d commit 85a15a7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
37 changes: 23 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ This package follows the official [Golang Release Policy](https://golang.org/doc
- [Alpine](#alpine)
- [Fedora](#fedora)
- [Ubuntu](#ubuntu)
- [Mac OSX](#mac-osx)
- [macOS](#mac-osx)
- [Windows](#windows)
- [Errors](#errors)
- [User Authentication](#user-authentication)
Expand Down Expand Up @@ -145,7 +145,7 @@ Click [here](https://golang.org/pkg/go/build/#hdr-Build_Constraints) for more in
If you wish to build this library with additional extensions / features, use the following command:

```bash
go build --tags "<FEATURE>"
go build -tags "<FEATURE>"
```

For available features, see the extension list.
Expand All @@ -154,7 +154,7 @@ When using multiple build tags, all the different tags should be space delimited
Example:

```bash
go build --tags "icu json1 fts5 secure_delete"
go build -tags "icu json1 fts5 secure_delete"
```

### Feature / Extension List
Expand Down Expand Up @@ -194,7 +194,7 @@ This package can be compiled for android.
Compile with:

```bash
go build --tags "android"
go build -tags "android"
```

For more information see [#201](https://github.com/mattn/go-sqlite3/issues/201)
Expand All @@ -219,8 +219,8 @@ This library can be cross-compiled.

In some cases you are required to the `CC` environment variable with the cross compiler.

## Cross Compiling from MAC OSX
The simplest way to cross compile from OSX is to use [musl-cross](https://github.com/FiloSottile/homebrew-musl-cross).
## Cross Compiling from macOS
The simplest way to cross compile from macOS is to use [xgo](https://github.com/karalabe/xgo).

Steps:
- Install [musl-cross](https://github.com/FiloSottile/homebrew-musl-cross) (`brew install FiloSottile/musl-cross/musl-cross`).
Expand All @@ -241,13 +241,13 @@ To compile this package on Linux, you must install the development tools for you
To compile under linux use the build tag `linux`.

```bash
go build --tags "linux"
go build -tags "linux"
```

If you wish to link directly to libsqlite3 then you can use the `libsqlite3` build tag.

```
go build --tags "libsqlite3 linux"
go build -tags "libsqlite3 linux"
```

### Alpine
Expand All @@ -270,34 +270,43 @@ sudo yum groupinstall "Development Tools" "Development Libraries"
sudo apt-get install build-essential
```

## Mac OSX
## macOS

OSX should have all the tools present to compile this package. If not, install XCode to add all the developers tools.
macOS should have all the tools present to compile this package. If not, install XCode to add all the developers tools.

Required dependency:

```bash
brew install sqlite3
```

For OSX, there is an additional package to install which is required if you wish to build the `icu` extension.
For macOS, there is an additional package to install which is required if you wish to build the `icu` extension.

This additional package can be installed with `homebrew`:

```bash
brew upgrade icu4c
```

To compile for Mac OSX:
To compile for macOS on x86:

```bash
go build --tags "darwin"
go build -tags "darwin amd64"
```

To compile for macOS on ARM chips:

```bash
go build -tags "darwin arm64"
```

If you wish to link directly to libsqlite3, use the `libsqlite3` build tag:

```
go build --tags "libsqlite3 darwin"
# x86
go build -tags "libsqlite3 darwin amd64"
# ARM
go build -tags "libsqlite3 darwin arm64"
```

Additional information:
Expand Down
6 changes: 4 additions & 2 deletions sqlite3_libsqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ package sqlite3
/*
#cgo CFLAGS: -DUSE_LIBSQLITE3
#cgo linux LDFLAGS: -lsqlite3
#cgo darwin LDFLAGS: -L/usr/local/opt/sqlite/lib -lsqlite3
#cgo darwin CFLAGS: -I/usr/local/opt/sqlite/include
#cgo darwin,amd64 LDFLAGS: -L/usr/local/opt/sqlite/lib -lsqlite3
#cgo darwin,amd64 CFLAGS: -I/usr/local/opt/sqlite/include
#cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/opt/sqlite/lib -lsqlite3
#cgo darwin,arm64 CFLAGS: -I/opt/homebrew/opt/sqlite/include
#cgo openbsd LDFLAGS: -lsqlite3
#cgo solaris LDFLAGS: -lsqlite3
#cgo windows LDFLAGS: -lsqlite3
Expand Down
6 changes: 4 additions & 2 deletions sqlite3_opt_icu.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ package sqlite3
/*
#cgo LDFLAGS: -licuuc -licui18n
#cgo CFLAGS: -DSQLITE_ENABLE_ICU
#cgo darwin CFLAGS: -I/usr/local/opt/icu4c/include
#cgo darwin LDFLAGS: -L/usr/local/opt/icu4c/lib
#cgo darwin,amd64 CFLAGS: -I/usr/local/opt/icu4c/include
#cgo darwin,amd64 LDFLAGS: -L/usr/local/opt/icu4c/lib
#cgo darwin,arm64 CFLAGS: -I/opt/homebrew/opt/icu4c/include
#cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/opt/icu4c/lib
#cgo openbsd LDFLAGS: -lsqlite3
*/
import "C"

0 comments on commit 85a15a7

Please sign in to comment.