Skip to content

Commit 6969fc1

Browse files
committed
Configure builder by default
1 parent fecb52b commit 6969fc1

5 files changed

Lines changed: 96 additions & 38 deletions

File tree

build/env/env.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os/exec"
77
"path/filepath"
88
"strings"
9+
"sync"
910

1011
"github.com/oasisprotocol/cli/cmd/common"
1112
)
@@ -86,6 +87,10 @@ type ContainerEnv struct {
8687
}
8788

8889
var containerCmds = []string{"docker", "podman"}
90+
var (
91+
containerCmdPath string
92+
containerCmdOnce sync.Once
93+
)
8994

9095
// NewContainerEnv creates a new Docker or Podman-based execution environment.
9196
func NewContainerEnv(image, baseDir, dirMount string) *ContainerEnv {
@@ -213,12 +218,20 @@ func (de *ContainerEnv) HasBinary(string) bool {
213218

214219
// getContainerCmd finds a working docker or podman command and returns its path.
215220
func getContainerCmd() string {
216-
for _, cmd := range containerCmds {
217-
if path, err := exec.LookPath(cmd); err == nil && path != "" {
218-
return path
221+
containerCmdOnce.Do(func() {
222+
for _, cmd := range containerCmds {
223+
if path, err := exec.LookPath(cmd); err == nil && path != "" {
224+
containerCmdPath = path
225+
return
226+
}
219227
}
220-
}
221-
return ""
228+
})
229+
return containerCmdPath
230+
}
231+
232+
// IsContainerRuntimeAvailable returns true if a container runtime (docker or podman) is available.
233+
func IsContainerRuntimeAvailable() bool {
234+
return getContainerCmd() != ""
222235
}
223236

224237
// IsAvailable implements ExecEnv.

build/rofl/artifacts.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package rofl
22

3+
// LatestBuilderImage is the latest builder container image to use when building ROFL apps.
4+
const LatestBuilderImage = "ghcr.io/oasisprotocol/rofl-dev:v0.5.0@sha256:31573686552abeb0edebc450f6872831f0006a6cf38220cef7e0789d4376c2c1"
5+
36
// LatestBasicArtifacts are the latest TDX ROFL basic app artifacts.
47
var LatestBasicArtifacts = ArtifactsConfig{
58
Firmware: "https://github.com/oasisprotocol/oasis-boot/releases/download/v0.6.2/ovmf.tdx.fd#db47100a7d6a0c1f6983be224137c3f8d7cb09b63bb1c7a5ee7829d8e994a42f",

build/sgxs/sgxs.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import (
1515
// It requires the `ftxsgx-elf2sgxs` utility to be installed.
1616
func Elf2Sgxs(buildEnv env.ExecEnv, elfSgxPath, sgxsPath string, heapSize, stackSize, threads uint64) (err error) {
1717
if elfSgxPath, err = buildEnv.PathToEnv(elfSgxPath); err != nil {
18-
return
18+
return err
1919
}
2020
if sgxsPath, err = buildEnv.PathToEnv(sgxsPath); err != nil {
21-
return
21+
return err
2222
}
2323

2424
args := []string{
@@ -31,7 +31,7 @@ func Elf2Sgxs(buildEnv env.ExecEnv, elfSgxPath, sgxsPath string, heapSize, stack
3131

3232
cmd := exec.Command("ftxsgx-elf2sgxs", args...)
3333
if err = buildEnv.WrapCommand(cmd); err != nil {
34-
return
34+
return err
3535
}
3636
if common.IsVerbose() {
3737
fmt.Println(cmd)

cmd/rofl/build/build.go

Lines changed: 65 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"maps"
88
"os"
99
"os/exec"
10+
"runtime"
1011
"slices"
12+
"strings"
1113

1214
"github.com/spf13/cobra"
1315
flag "github.com/spf13/pflag"
@@ -88,41 +90,41 @@ var (
8890
// Ensure deterministic umask for builds.
8991
setUmask(0o002)
9092

93+
// Determine builder image to use.
94+
builderImage := ""
95+
if manifest.Artifacts != nil {
96+
builderImage = strings.TrimSpace(manifest.Artifacts.Builder)
97+
if manifest.Artifacts.Builder != "" && builderImage == "" {
98+
return fmt.Errorf("builder image is empty after trimming whitespace")
99+
}
100+
}
101+
// Native builds are only supported on Linux.
102+
nativeBuildSupported := runtime.GOOS == "linux" && runtime.GOARCH == "amd64"
103+
91104
var buildEnv env.ExecEnv
92105
switch {
93-
case manifest.Artifacts == nil || manifest.Artifacts.Builder == "" || noContainer:
106+
case noContainer:
107+
// Force native build regardless of manifest.
108+
if !nativeBuildSupported {
109+
return fmt.Errorf("native ROFL builds are only supported on linux/amd64; remove --no-container to use containerized builds on %s/%s", runtime.GOOS, runtime.GOARCH)
110+
}
94111
buildEnv = env.NewNativeEnv()
112+
case builderImage == "":
113+
// No builder image specified.
114+
if nativeBuildSupported {
115+
buildEnv = env.NewNativeEnv()
116+
} else {
117+
return fmt.Errorf("no builder image specified in manifest; run `oasis rofl upgrade` to add the default builder or set artifacts.builder")
118+
}
95119
default:
96-
var baseDir string
97-
baseDir, err = env.GetBasedir()
98-
if err != nil {
99-
return fmt.Errorf("failed to determine base directory: %w", err)
120+
// Builder image specified.
121+
if !env.IsContainerRuntimeAvailable() {
122+
return fmt.Errorf("builder specified in manifest but no container runtime (docker or podman) is available")
100123
}
101-
102-
containerEnv := env.NewContainerEnv(
103-
manifest.Artifacts.Builder,
104-
baseDir,
105-
"/src",
106-
)
107-
containerEnv.AddDirectory(tmpDir)
108-
buildEnv = containerEnv
109-
110-
if buildEnv.IsAvailable() {
111-
fmt.Printf("Initializing build environment...\n")
112-
// Run a dummy command to make sure that all necessary Docker layers
113-
// for the build environment are downloaded at the start instead of
114-
// later in the build process.
115-
// Also pipe all output from the process to stdout/stderr, so the user
116-
// can follow the progress in real-time.
117-
cmd := exec.Command("true")
118-
cmd.Stdout = os.Stdout
119-
cmd.Stderr = os.Stderr
120-
if err = buildEnv.WrapCommand(cmd); err != nil {
121-
return fmt.Errorf("unable to wrap command: %w", err)
122-
}
123-
if err = cmd.Run(); err != nil {
124-
return fmt.Errorf("failed to initialize build environment: %w", err)
125-
}
124+
fmt.Printf("Using container build environment (image: %s)\n", builderImage)
125+
buildEnv, err = setupContainerEnv(builderImage, tmpDir)
126+
if err != nil {
127+
return err
126128
}
127129
}
128130

@@ -304,6 +306,39 @@ var (
304306
}
305307
)
306308

309+
// setupContainerEnv creates and initializes a container build environment.
310+
func setupContainerEnv(builderImage, tmpDir string) (env.ExecEnv, error) {
311+
baseDir, err := env.GetBasedir()
312+
if err != nil {
313+
return nil, fmt.Errorf("failed to determine base directory: %w", err)
314+
}
315+
316+
containerEnv := env.NewContainerEnv(
317+
builderImage,
318+
baseDir,
319+
"/src",
320+
)
321+
containerEnv.AddDirectory(tmpDir)
322+
323+
fmt.Printf("Initializing build environment...\n")
324+
// Run a dummy command to make sure that all necessary Docker layers
325+
// for the build environment are downloaded at the start instead of
326+
// later in the build process.
327+
// Also pipe all output from the process to stdout/stderr, so the user
328+
// can follow the progress in real-time.
329+
cmd := exec.Command("true")
330+
cmd.Stdout = os.Stdout
331+
cmd.Stderr = os.Stderr
332+
if err = containerEnv.WrapCommand(cmd); err != nil {
333+
return nil, fmt.Errorf("unable to wrap command: %w", err)
334+
}
335+
if err = cmd.Run(); err != nil {
336+
return nil, fmt.Errorf("failed to initialize build environment with image %s (ensure the image is accessible and your container runtime can pull it): %w", builderImage, err)
337+
}
338+
339+
return containerEnv, nil
340+
}
341+
307342
func setupBuildEnv(deployment *buildRofl.Deployment, npa *common.NPASelection) {
308343
// Configure app ID.
309344
os.Setenv("ROFL_APP_ID", deployment.AppID)

cmd/rofl/mgmt.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ var (
128128
manifest.Artifacts = &artifacts
129129
default:
130130
}
131+
if manifest.Artifacts != nil {
132+
manifest.Artifacts.Builder = buildRofl.LatestBuilderImage
133+
}
131134
default:
132135
}
133136

@@ -565,9 +568,13 @@ var (
565568
latestArtifacts = buildRofl.LatestContainerArtifacts // Copy.
566569
default:
567570
}
571+
latestArtifacts.Builder = buildRofl.LatestBuilderImage
568572
default:
569573
}
570574

575+
if manifest.Artifacts == nil {
576+
manifest.Artifacts = &buildRofl.ArtifactsConfig{}
577+
}
571578
if !manifest.Artifacts.UpgradeTo(&latestArtifacts) {
572579
fmt.Printf("Artifacts already up-to-date.\n")
573580
return

0 commit comments

Comments
 (0)