Skip to content

Commit

Permalink
Allow to set extra build flags. (#45)
Browse files Browse the repository at this point in the history
* Allow to set extra build flags.

Cargo Lambda Build has additional flags that can be set to modify the build output. This new bundling option allows to set extra flags that are not provided by other options already.

Signed-off-by: David Calavera <[email protected]>

* Remove option that was never released.

Signed-off-by: David Calavera <[email protected]>

---------

Signed-off-by: David Calavera <[email protected]>
  • Loading branch information
calavera authored May 3, 2024
1 parent dfccba2 commit 3246e84
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 36 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ new RustFunction(this, 'Rust function', {
});
```

### Cargo Lambda Build flags

Use the `cargoLambdaFlags` option to add additional flags to the `cargo lambda build` command that's executed to bundle your function. You don't need to use this flag to set options like the target architecture or the binary to compile, since the construct infers those from other props.

If these flags include a `--target` flag, it will override the `architecture` option. If these flags include a `--release` or `--debug` flag, it will override the CDK's debug option.

```ts
import { RustFunction } from 'cargo-lambda-cdk';

new RustFunction(this, 'Rust function', {
manifestPath: 'path/to/package/directory/with/Cargo.toml',
bundling: {
cargoLambdaFlags: [
'--target',
'x86_64-unknown-linux-musl',
'--debug',
'--disable-optimizations',
],
},
});
```

### Docker

To force bundling in a docker container even if `Cargo Lambda` is available in your environment, set the `forcedDockerBundling` prop to `true`. This is useful if you want to make sure that your function is built in a consistent Lambda compatible environment.
Expand Down
26 changes: 14 additions & 12 deletions src/bundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export interface BundlingProps extends BundlingOptions {
readonly lambdaExtension?: boolean;

/**
* Whether to disable optimizations (`--disable-optimizations` in Cargo Lambda).
* Set a list of flags to pass to `cargo lambda build`.
*/
readonly disableOptimizations?: boolean;
readonly cargoLambdaFlags?: string[];
}

interface CommandOptions {
Expand All @@ -52,7 +52,7 @@ interface CommandOptions {
readonly osPlatform: NodeJS.Platform;
readonly architecture?: Architecture;
readonly lambdaExtension?: boolean;
readonly disableOptimizations?: boolean;
readonly cargoLambdaFlags: string[];
readonly manifest: Manifest;
}

Expand Down Expand Up @@ -108,16 +108,18 @@ export class Bundling implements cdk.BundlingOptions {

const manifest = getManifest(props.manifestPath);

const cargoLambdaFlags = props.cargoLambdaFlags ?? [];

const osPlatform = platform();
const bundlingCommand = this.createBundlingCommand({
osPlatform,
manifest,
cargoLambdaFlags,
outputDir: cdk.AssetStaging.BUNDLING_OUTPUT_DIR,
inputDir: cdk.AssetStaging.BUNDLING_INPUT_DIR,
binaryName: props.binaryName,
architecture: props.architecture,
lambdaExtension: props.lambdaExtension,
disableOptimizations: props.disableOptimizations,
});

this.command = ['bash', '-c', bundlingCommand];
Expand All @@ -130,11 +132,11 @@ export class Bundling implements cdk.BundlingOptions {
osPlatform,
manifest,
outputDir,
cargoLambdaFlags,
inputDir: projectRoot,
binaryName: props.binaryName,
architecture: props.architecture,
lambdaExtension: props.lambdaExtension,
disableOptimizations: props.disableOptimizations,
});
};

Expand Down Expand Up @@ -174,20 +176,20 @@ export class Bundling implements cdk.BundlingOptions {
'cargo',
'lambda',
'build',
'--release',
'--lambda-dir',
props.outputDir,
];

if (props.lambdaExtension) {
buildBinary.push('--extension');
if (!props.cargoLambdaFlags.includes('--release') &&
!props.cargoLambdaFlags.includes('--debug')) {
buildBinary.push('--release');
}

if (props.disableOptimizations) {
buildBinary.push('--disable-optimizations');
if (props.lambdaExtension) {
buildBinary.push('--extension');
}

if (props.architecture) {
if (props.architecture && !props.cargoLambdaFlags.includes('--target')) {
const targetFlag = props.architecture.name == Architecture.ARM_64.name ? '--arm64' : '--x86-64';
buildBinary.push(targetFlag);
}
Expand Down Expand Up @@ -224,7 +226,7 @@ export class Bundling implements cdk.BundlingOptions {
buildBinary.push(packageName);
}

const command = buildBinary.join(' ');
const command = buildBinary.concat(props.cargoLambdaFlags).join(' ');

return chain([
...this.props.commandHooks?.beforeBundling(props.inputDir, props.outputDir) ?? [],
Expand Down
6 changes: 0 additions & 6 deletions src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ export interface RustFunctionProps extends FunctionOptions {
* @default - use default bundling options
*/
readonly bundling?: BundlingOptions;

/**
* Whether to disable optimizations (`--disable-optimizations` in Cargo Lambda).
*/
readonly disableOptimizations?: boolean;
}

/**
Expand All @@ -64,7 +59,6 @@ export class RustFunction extends Function {
...bundling,
manifestPath,
binaryName: props?.binaryName,
disableOptimizations: props?.disableOptimizations,
}),
handler: 'bootstrap',
});
Expand Down
12 changes: 12 additions & 0 deletions test/bundlingOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,16 @@ describe('bundlingOptionsOverrideDefaults', () => {

expect((bundlingOptions as any).options.bundling.bundlingFileAccess).toEqual(cdk.BundlingFileAccess.VOLUME_COPY);
});

describe('Add Cargo Lambda Build flags', () => {
const bundlingOptions = Bundling.bundle({
manifestPath: getTestManifestPath(),
forcedDockerBundling: true,
cargoLambdaFlags: ['--disable-optimizations'],
});

const command = 'cargo lambda build --lambda-dir /asset-output --release --flatten simple-package --disable-optimizations';

expect((bundlingOptions as any).options.bundling.command).toContain(command);
})
});
18 changes: 0 additions & 18 deletions test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,4 @@ describe('CargoLambda.RustFunction', () => {
app.synth();
});
});

describe('With disableOptimizations', () => {
const app = new App();
const stack = new Stack(app);
const testSource = join(__dirname, 'fixtures/single-package');

new RustFunction(stack, 'rust function', {
manifestPath: testSource,
bundling: {
forcedDockerBundling,
},
disableOptimizations: true,
});

test('bundle function', () => {
app.synth();
});
});
});

0 comments on commit 3246e84

Please sign in to comment.