Skip to content

Commit

Permalink
cgo: let the go toolchain build the c library (#20473)
Browse files Browse the repository at this point in the history
let the go toolchain build c library to avoid dynamic linking with `-lmo`

Approved by: @zhangxu19830126, @aunjgr
  • Loading branch information
reusee authored Dec 4, 2024
1 parent f7ad01c commit 81f10ad
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pb: vendor-build generate-pb fmt
RACE_OPT :=
DEBUG_OPT :=
CGO_DEBUG_OPT :=
CGO_OPTS=CGO_CFLAGS="-I$(ROOT_DIR)/cgo " CGO_LDFLAGS="-L$(ROOT_DIR)/cgo -lm -lmo"
CGO_OPTS :=
GOLDFLAGS=-ldflags="-X '$(GO_MODULE)/pkg/version.GoVersion=$(GO_VERSION)' -X '$(GO_MODULE)/pkg/version.BranchName=$(BRANCH_NAME)' -X '$(GO_MODULE)/pkg/version.CommitID=$(LAST_COMMIT_ID)' -X '$(GO_MODULE)/pkg/version.BuildTime=$(BUILD_TIME)' -X '$(GO_MODULE)/pkg/version.Version=$(MO_VERSION)'"
TAGS :=

Expand All @@ -108,7 +108,7 @@ cgo:

# build mo-service binary
.PHONY: build
build: config cgo
build: config
$(info [Build binary])
$(CGO_OPTS) go build $(TAGS) $(RACE_OPT) $(GOLDFLAGS) $(DEBUG_OPT) -o $(BIN_NAME) ./cmd/mo-service

Expand Down Expand Up @@ -139,7 +139,7 @@ musl:

# build mo-tool
.PHONY: mo-tool
mo-tool: config cgo
mo-tool: config
$(info [Build mo-tool tool])
$(CGO_OPTS) go build -o mo-tool ./cmd/mo-tool

Expand All @@ -158,7 +158,7 @@ debug: build
# Excluding frontend test cases temporarily
# Argument SKIP_TEST to skip a specific go test
.PHONY: ut
ut: config cgo
ut: config
$(info [Unit testing])
ifeq ($(UNAME_S),Darwin)
@cd optools && ./run_ut.sh UT $(SKIP_TEST)
Expand Down Expand Up @@ -240,7 +240,7 @@ install-static-check-tools:
@go install github.com/apache/skywalking-eyes/cmd/[email protected]

.PHONY: static-check
static-check: config cgo err-check
static-check: config err-check
$(CGO_OPTS) go vet -vettool=`which molint` ./...
$(CGO_OPTS) license-eye -c .licenserc.yml header check
$(CGO_OPTS) license-eye -c .licenserc.yml dep check
Expand Down
21 changes: 21 additions & 0 deletions cgo/lib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2024 Matrix Origin
//
// 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 cgo

/*
#cgo CFLAGS: -O3
#cgo LDFLAGS: -lm
*/
import "C"
10 changes: 5 additions & 5 deletions pkg/common/bitmap/cbitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
package bitmap

/*
#include "mo.h"
#cgo CFLAGS: -I../../../cgo
#cgo LDFLAGS: -L../../../cgo -lmo -lm
#include "../../../cgo/mo.h"
*/
import "C"
import "unsafe"
import (
_ "github.com/matrixorigin/matrixone/cgo"
"unsafe"
)

func (n *Bitmap) cPtr() *C.uint64_t {
return (*C.uint64_t)(unsafe.Pointer(&n.data[0]))
Expand Down

0 comments on commit 81f10ad

Please sign in to comment.