|
4 | 4 | *--------------------------------------------------------------------------------------------*/ |
5 | 5 |
|
6 | 6 | import * as semver from 'semver'; |
| 7 | +import { ExecFunction, runCommandNoPty } from '../spec-common/commonUtils'; |
| 8 | +import { Log } from '../spec-utils/log'; |
7 | 9 | import { Mount } from '../spec-configuration/containerFeaturesConfiguration'; |
8 | 10 |
|
9 | 11 |
|
@@ -260,6 +262,37 @@ export function ensureDockerfileHasFinalStageName(dockerfile: string, defaultLas |
260 | 262 | return { lastStageName: defaultLastStageName, modifiedDockerfile: modifiedDockerfile }; |
261 | 263 | } |
262 | 264 |
|
| 265 | +/** |
| 266 | + * Preprocess a Dockerfile.in file using the host cpp tool. |
| 267 | + * This is needed when using Podman, which supports cpp preprocessing of .in files natively. |
| 268 | + * Throws a clear error if cpp is not available on the host. |
| 269 | + */ |
| 270 | +export async function preprocessDockerfileIn(dockerfilePath: string, exec: ExecFunction, output: Log): Promise<string> { |
| 271 | + let result: { stdout: Buffer; stderr: Buffer }; |
| 272 | + try { |
| 273 | + result = await runCommandNoPty({ |
| 274 | + exec, |
| 275 | + cmd: 'cpp', |
| 276 | + // -undef: do not predefine platform/compiler macros |
| 277 | + // -fdirectives-only: only process directives, do not expand macros |
| 278 | + // -w: suppress warnings |
| 279 | + // -P: suppress linemarker output lines |
| 280 | + args: ['-undef', '-fdirectives-only', '-w', '-P', dockerfilePath], |
| 281 | + output, |
| 282 | + }); |
| 283 | + } catch (err: any) { |
| 284 | + if (err?.code === 'ENOENT' || err?.message?.includes('ENOENT')) { |
| 285 | + throw new Error( |
| 286 | + `Preprocessing '${dockerfilePath}' requires 'cpp', but it was not found on the host. ` + |
| 287 | + `Please install cpp (e.g. "sudo apt-get install cpp") to use Dockerfile.in files with Podman.` |
| 288 | + ); |
| 289 | + } |
| 290 | + const stderrText = err?.stderr ? `\n${(err.stderr as Buffer).toString()}` : ''; |
| 291 | + throw new Error(`Failed to preprocess '${dockerfilePath}' using cpp: ${err?.message || String(err)}${stderrText}`); |
| 292 | + } |
| 293 | + return result.stdout.toString(); |
| 294 | +} |
| 295 | + |
263 | 296 | export function supportsBuildContexts(dockerfile: Dockerfile) { |
264 | 297 | const version = dockerfile.preamble.version; |
265 | 298 | if (!version) { |
|
0 commit comments