Files
Dmitry Kovba 9a597eb6aa Add the support for ARG in the native builder parser (#516)
This PR adds the support for the ARG instruction in the native builder
parser, implements the support for different kinds of ARGs, and performs
the substitution of ARG variables in the supported instructions.
Resolves https://github.com/apple/container/issues/437.

Some features are currently blocked and not included into this PR:
- [Native builder: add the current target platform we're building use it
to set automatic platform ARGs
#522](https://github.com/apple/container/issues/522)
- [Native builder: add the support for the BuildKit built-in ARGs
#523](https://github.com/apple/container/issues/523)
- [Native builder: ensure pre-defined ARGs are excluded from the output
of the history #524](https://github.com/apple/container/issues/524)
- [Native builder: add the support for stage references and ARG
inheritance #525](https://github.com/apple/container/issues/525)
2025-08-20 14:57:24 -07:00

82 lines
2.5 KiB
Swift

//===----------------------------------------------------------------------===//
// Copyright © 2025 Apple Inc. and the container project authors. All rights reserved.
//
// 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
//
// https://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.
//===----------------------------------------------------------------------===//
import ContainerBuildIR
import ContainerizationOCI
/// DockerInstruction represents a single docker instruction with its given options
/// and arguments. Instructions are "visited" to add to a build graph.
protocol DockerInstruction: Sendable, Equatable {
func accept(_ visitor: DockerInstructionVisitor) throws
}
enum FromOptions: String {
case platform = "--platform"
}
struct FromInstruction: DockerInstruction {
let image: String
let platform: Platform?
let stageName: String?
init(image: String, platform: String? = nil, stageName: String? = nil) throws {
self.image = image
var platformSpec = Platform.current
if let platform = platform {
platformSpec = try Platform(from: platform)
}
self.platform = platformSpec
self.stageName = stageName
}
func accept(_ visitor: DockerInstructionVisitor) throws {
try visitor.visit(self)
}
}
/// DockerInstructionName defines a dockerfile instruction such as FROM, RUN, etc.
enum DockerInstructionName: String {
case FROM = "from"
case RUN = "run"
case COPY = "copy"
case CMD = "cmd"
case LABEL = "label"
case EXPOSE = "expose"
case ARG = "arg"
}
/// DockerKeyword defines words that are used as keywords within a line of a dockerfile
/// to provide additional instruction
enum DockerKeyword: String {
case AS = "as"
}
struct CMDInstruction: DockerInstruction {
let command: Command
func accept(_ visitor: DockerInstructionVisitor) throws {
try visitor.visit(self)
}
}
struct LabelInstruction: DockerInstruction {
let labels: [String: String]
func accept(_ visitor: DockerInstructionVisitor) throws {
try visitor.visit(self)
}
}