Skip to content

Commit eab3725

Browse files
committed
Simplify build from a clean repo
The following was done: 1. Magefiles now require `mage` tag to be built; 2. Dependency for fileshare main packege on libdrop was removed; 3. `build_rust.sh` split in order to allow either of the libraries or none to be compiled via mage; 4. Determining version from the changelog as a fallback if tags are not fetched yet; 5. By default no features are specified unless defined otherwise; Signed-off-by: Savolro <[email protected]>
1 parent 81f754e commit eab3725

File tree

13 files changed

+188
-57
lines changed

13 files changed

+188
-57
lines changed

ci/build_libdrop.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
set -euxo pipefail
4+
5+
source "${WORKDIR}/ci/build_rust.sh"
6+
7+
clone_if_absent "https://github.com/NordSecurity/libdrop.git" "${LIBDROP_VERSION}" "${WORKDIR}/build/foss"
8+
9+
# libdrop does not define configuration for linkers for different architectures
10+
linkers_config=$(
11+
cat <<EOF
12+
[target.x86_64-unknown-linux-gnu]
13+
linker = "x86_64-linux-gnu-gcc"
14+
15+
[target.i686-unknown-linux-gnu]
16+
linker = "i686-linux-gnu-gcc"
17+
18+
[target.aarch64-unknown-linux-gnu]
19+
linker = "aarch64-linux-gnu-gcc"
20+
21+
[target.armv7-unknown-linux-gnueabihf]
22+
linker = "arm-linux-gnueabihf-gcc"
23+
24+
[target.arm-unknown-linux-gnueabi]
25+
linker = "arm-linux-gnueabi-gcc"
26+
EOF
27+
)
28+
mkdir -p "${WORKDIR}/build/foss/libdrop/.cargo"
29+
echo "${linkers_config}" >"${WORKDIR}/build/foss/libdrop/config.toml"
30+
31+
build_rust "${WORKDIR}/build/foss/libdrop"
32+
link_so_files "libdrop"
33+
34+
populate_current_ver "${lib_root}/current" "${lib_root}/libtelio/${LIBTELIO_VERSION}" "libtelio.so"
35+
populate_current_ver "${lib_root}/current" "${lib_root}/libdrop/${LIBDROP_VERSION}" "libnorddrop.so"

ci/build_libtelio.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
set -euxo pipefail
4+
5+
source "${WORKDIR}/ci/build_rust.sh"
6+
7+
mkdir -p "${WORKDIR}/build/foss"
8+
9+
clone_if_absent "https://github.com/NordSecurity/libtelio.git" "${LIBTELIO_VERSION}" "${WORKDIR}/build/foss"
10+
11+
rm -rf "${lib_root}/current"
12+
13+
# BYPASS_LLT_SECRETS is needed for libtelio builds
14+
BYPASS_LLT_SECRETS=1 build_rust "${WORKDIR}/build/foss/libtelio"
15+
link_so_files "libtelio"
16+
17+
populate_current_ver "${lib_root}/current" "${lib_root}/libtelio/${LIBTELIO_VERSION}" "libtelio.so"

ci/build_rust.sh

100755100644
Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#!/usr/bin/env bash
2-
set -euxo pipefail
3-
42
source "${WORKDIR}/ci/export_lib_versions.sh"
53
source "${WORKDIR}/ci/populate_current_lib_ver.sh"
64

@@ -80,45 +78,3 @@ function link_so_files() {
8078
ln -sfn "${target_arch}/release" "${target_dir}/${arch}"
8179
done
8280
}
83-
84-
mkdir -p "${WORKDIR}/build/foss"
85-
86-
# ====================[ Build libtelio from source ]=========================
87-
clone_if_absent "https://github.com/NordSecurity/libtelio.git" "${LIBTELIO_VERSION}" "${WORKDIR}/build/foss"
88-
89-
rm -rf "${lib_root}/current"
90-
91-
# BYPASS_LLT_SECRETS is needed for libtelio builds
92-
BYPASS_LLT_SECRETS=1 build_rust "${WORKDIR}/build/foss/libtelio"
93-
link_so_files "libtelio"
94-
95-
# ====================[ Build libdrop from source ]==========================
96-
clone_if_absent "https://github.com/NordSecurity/libdrop.git" "${LIBDROP_VERSION}" "${WORKDIR}/build/foss"
97-
98-
# libdrop does not define configuration for linkers for different architectures
99-
linkers_config=$(
100-
cat <<EOF
101-
[target.x86_64-unknown-linux-gnu]
102-
linker = "x86_64-linux-gnu-gcc"
103-
104-
[target.i686-unknown-linux-gnu]
105-
linker = "i686-linux-gnu-gcc"
106-
107-
[target.aarch64-unknown-linux-gnu]
108-
linker = "aarch64-linux-gnu-gcc"
109-
110-
[target.armv7-unknown-linux-gnueabihf]
111-
linker = "arm-linux-gnueabihf-gcc"
112-
113-
[target.arm-unknown-linux-gnueabi]
114-
linker = "arm-linux-gnueabi-gcc"
115-
EOF
116-
)
117-
mkdir -p "${WORKDIR}/build/foss/libdrop/.cargo"
118-
echo "${linkers_config}" >"${WORKDIR}/build/foss/libdrop/config.toml"
119-
120-
build_rust "${WORKDIR}/build/foss/libdrop"
121-
link_so_files "libdrop"
122-
123-
populate_current_ver "${lib_root}/current" "${lib_root}/libtelio/${LIBTELIO_VERSION}" "libtelio.so"
124-
populate_current_ver "${lib_root}/current" "${lib_root}/libdrop/${LIBDROP_VERSION}" "libnorddrop.so"

ci/compile.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export CGO_LDFLAGS="${CGO_LDFLAGS:-""} -Wl,-z,relro,-z,now"
5656
[ "${ARCH}" == "armhf" ] && export GOARM=7
5757

5858
# In order to enable additional features, provide `FEATURES` environment variable
59-
tags="${FEATURES:-"telio drop"}"
59+
tags="${FEATURES:-"none"}"
6060

6161
source "${WORKDIR}"/ci/set_bindings_version.sh libtelio
6262
source "${WORKDIR}"/ci/set_bindings_version.sh libdrop

ci/env.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ else
2828
export REVISION
2929

3030
# '+' character is chosen because '_' is not allowed in .deb packages and '-' is not allowed in .rpm packages
31-
VERSION="$(git tag -l --sort=-v:refname | grep "^[0-9]\+\.[0-9]\+\.[0-9]\+$" | sed -n 1p)+${REVISION}"
31+
VERSION=${VERSION:-"$(git tag -l --sort=-v:refname | grep "^[0-9]\+\.[0-9]\+\.[0-9]\+$" | sed -n 1p)+${REVISION}"}
3232
export VERSION
3333
fi

cmd/fileshare/fileshare_drop.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:build drop
2+
3+
package main
4+
5+
import (
6+
"github.com/NordSecurity/nordvpn-linux/fileshare"
7+
"github.com/NordSecurity/nordvpn-linux/fileshare/libdrop"
8+
)
9+
10+
func newFileshare(
11+
eventCallback fileshare.EventCallback,
12+
eventsDBPath string,
13+
isProd bool,
14+
pubkeyFunc func(string) []byte,
15+
privateKey string,
16+
storagePath string,
17+
) (fileshare.Fileshare, fileshare.Storage, error) {
18+
fs, err := libdrop.New(
19+
eventCallback,
20+
eventsDBPath,
21+
isProd,
22+
pubkeyFunc,
23+
privateKey,
24+
storagePath,
25+
)
26+
return fs, fs, err
27+
}

cmd/fileshare/fileshare_nodrop.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//go:build !drop
2+
3+
package main
4+
5+
import "github.com/NordSecurity/nordvpn-linux/fileshare"
6+
7+
func newFileshare(
8+
eventCallback fileshare.EventCallback,
9+
eventsDBPath string,
10+
isProd bool,
11+
pubkeyFunc func(string) []byte,
12+
privateKey string,
13+
storagePath string,
14+
) (fileshare.Fileshare, fileshare.Storage, error) {
15+
return nil, nil, nil
16+
}

cmd/fileshare/main.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"github.com/NordSecurity/nordvpn-linux/fileshare"
2121
"github.com/NordSecurity/nordvpn-linux/fileshare/fileshare_process"
2222
"github.com/NordSecurity/nordvpn-linux/fileshare/fileshare_startup"
23-
"github.com/NordSecurity/nordvpn-linux/fileshare/libdrop"
2423
"github.com/NordSecurity/nordvpn-linux/fileshare/storage"
2524
"github.com/NordSecurity/nordvpn-linux/internal"
2625
meshpb "github.com/NordSecurity/nordvpn-linux/meshnet/pb"
@@ -179,7 +178,7 @@ func main() {
179178
os.Exit(int(childprocess.CodeFailedToEnable))
180179
}
181180

182-
fileshareImplementation, err := libdrop.New(
181+
fileshareImplementation, fileshareStorage, err := newFileshare(
183182
eventManager,
184183
eventsDBPath,
185184
internal.IsProdEnv(Environment),
@@ -199,9 +198,9 @@ func main() {
199198

200199
eventManager.SetFileshare(fileshareImplementation)
201200
if legacyStoragePath != "" {
202-
eventManager.SetStorage(storage.NewCombined(legacyStoragePath, fileshareImplementation))
201+
eventManager.SetStorage(storage.NewCombined(legacyStoragePath, fileshareStorage))
203202
} else {
204-
eventManager.SetStorage(storage.NewLibdrop(fileshareImplementation))
203+
eventManager.SetStorage(storage.NewLibdrop(fileshareStorage))
205204
}
206205

207206
settings, err := daemonClient.Settings(context.Background(), &daemonpb.Empty{})
@@ -225,7 +224,7 @@ func main() {
225224

226225
err = fileshareImplementation.Enable(meshnetIP)
227226
if err != nil {
228-
if errors.Is(err, libdrop.ErrLAddressAlreadyInUse) {
227+
if errors.Is(err, fileshare.ErrAddressAlreadyInUse) {
229228
log.Println(internal.ErrorPrefix, "mesh already in use:", err)
230229
os.Exit(int(childprocess.CodeAddressAlreadyInUse))
231230
}

fileshare/fileshare.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package fileshare
33

44
import (
5+
"errors"
56
"net/netip"
67
"time"
78

@@ -13,6 +14,8 @@ const (
1314
TransferFileLimit = 1000
1415
)
1516

17+
var ErrAddressAlreadyInUse = errors.New("address already in use")
18+
1619
// Fileshare defines a set of operations that any type that wants to act as a fileshare service
1720
// must implement.
1821
type Fileshare interface {

fileshare/libdrop/libdrop.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
"google.golang.org/protobuf/types/known/timestamppb"
1919
)
2020

21-
var ErrLAddressAlreadyInUse = errors.New("address already in use")
22-
2321
// Fileshare is the main functional filesharing implementation using norddrop library.
2422
// Thread safe.
2523
type Fileshare struct {
@@ -226,7 +224,7 @@ func (f *Fileshare) Enable(listenAddr netip.Addr) (err error) {
226224

227225
if err = f.start(listenAddr, f.eventsDbPath, f.isProd, f.storagePath); err != nil {
228226
if errors.Is(err, norddrop.ErrLibdropErrorAddrInUse) {
229-
return ErrLAddressAlreadyInUse
227+
return fileshare.ErrAddressAlreadyInUse
230228
}
231229
return fmt.Errorf("starting drop: %w", err)
232230
}

magefiles/docker.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build mage
2+
13
package main
24

35
import (

magefiles/mage.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build mage
2+
13
package main
24

35
import (
@@ -458,8 +460,22 @@ func (Build) Rust(ctx context.Context) error {
458460

459461
env["WORKDIR"] = cwd
460462
env["ARCHS_RUST"] = env["ARCH"]
463+
features, ok := env["FEATURES"]
464+
if !ok {
465+
features = "telio drop"
466+
}
461467

462-
return sh.RunWith(env, "ci/build_rust.sh")
468+
if strings.Contains(features, "telio") {
469+
if err := sh.RunWith(env, "ci/build_libtelio.sh"); err != nil {
470+
return err
471+
}
472+
}
473+
if strings.Contains(features, "drop") {
474+
if err := sh.RunWith(env, "ci/build_libdrop.sh"); err != nil {
475+
return err
476+
}
477+
}
478+
return nil
463479
}
464480

465481
// Builds rust dependencies using Docker builder

magefiles/scripts.go

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
//go:build mage
2+
13
package main
24

35
import (
6+
"errors"
7+
"fmt"
48
"go/build"
9+
"io/fs"
510
"log"
611
"os"
12+
"slices"
713
"strings"
814

15+
"github.com/coreos/go-semver/semver"
916
"github.com/magefile/mage/sh"
1017
)
1118

@@ -27,7 +34,7 @@ func getEnv() (map[string]string, error) {
2734

2835
func readVarsFromFile(filename string) (map[string]string, error) {
2936
content, err := os.ReadFile(filename)
30-
if err != nil {
37+
if err != nil && !errors.Is(err, os.ErrNotExist) {
3138
return nil, err
3239
}
3340

@@ -58,7 +65,7 @@ func getGitInfo() (*gitInfo, error) {
5865
return nil, err
5966
}
6067

61-
version, err := sh.Output("git", "describe", "--tags", "--abbrev=0")
68+
version, err := getLatestVersion()
6269
if err != nil {
6370
return nil, err
6471
}
@@ -69,6 +76,61 @@ func getGitInfo() (*gitInfo, error) {
6976
}, nil
7077
}
7178

79+
func getLatestVersion() (string, error) {
80+
version, err := getLatestVersionGit()
81+
if err != nil {
82+
return "", err
83+
}
84+
if version != "" {
85+
return version, nil
86+
}
87+
fmt.Println("Unable to determine version from git tags. Using changelog.")
88+
89+
version, err = getLatestVersionChangelog()
90+
if err != nil {
91+
return "", err
92+
}
93+
if version != "" {
94+
return version, nil
95+
}
96+
fmt.Println("Unable to determine version the changelog. Using 0.0.0")
97+
98+
return "0.0.0", nil
99+
}
100+
101+
func getLatestVersionGit() (string, error) {
102+
version, err := sh.Output("git", "describe", "--tags", "--abbrev=0")
103+
if err != nil && !strings.Contains(err.Error(), "exit code 128") {
104+
return "", err
105+
}
106+
return version, nil
107+
}
108+
109+
func getLatestVersionChangelog() (string, error) {
110+
changelogs, err := os.ReadDir("contrib/changelog/prod")
111+
if err != nil {
112+
return "", err
113+
}
114+
changelog := slices.MaxFunc(changelogs, func(a fs.DirEntry, b fs.DirEntry) int {
115+
return changelogMdToSemver(a.Name()).Compare(changelogMdToSemver(b.Name()))
116+
})
117+
if changelog == nil {
118+
return "", nil
119+
}
120+
return changelogMdToSemver(changelog.Name()).String(), nil
121+
}
122+
123+
func changelogMdToSemver(filename string) semver.Version {
124+
if !strings.HasSuffix(filename, ".md") {
125+
return semver.Version{}
126+
}
127+
ver, err := semver.NewVersion(strings.Split(strings.TrimSuffix(filename, ".md"), "_")[0])
128+
if err != nil && ver == nil {
129+
return semver.Version{}
130+
}
131+
return *ver
132+
}
133+
72134
func mergeMaps(m1, m2 map[string]string) map[string]string {
73135
result := make(map[string]string)
74136

0 commit comments

Comments
 (0)