diff --git a/doc/images/gen_spm_module.png b/doc/images/gen_spm_module.png new file mode 100644 index 000000000..0798c37b6 Binary files /dev/null and b/doc/images/gen_spm_module.png differ diff --git a/doc/swift-target.md b/doc/swift-target.md index 57a8f8022..4f4e6e7c1 100644 --- a/doc/swift-target.md +++ b/doc/swift-target.md @@ -6,7 +6,6 @@ To use ANTLR4 Swift target in production environment, make sure to turn on compi Conclusion is, you need to turn on `release` mode (which will have all the optimization pre configured for you) so the ANTLR4 Swift target can have reasonable parsing speed. - ## Install ANTLR4 Make sure you have the ANTLR @@ -23,10 +22,22 @@ $ antlr4 -Dlanguage=Swift MyGrammar.g4 For a full list of antlr4 tool options, please visit the [tool documentation page](tool-options.md). - ## Build your Swift project with ANTLR runtime -The following instructions assume Xcode as the IDE. +### Note + +We use __boot.py__ script located at the root of the Swift runtime folder +`antlr4/runtime/Swift` to provide additional support for both Xcode-based +projects and SPM-based projects. Below sections are organized for both of +the flavors. If you want to quickly get started, try: + +``` +python boot.py --help +``` + +for information about this script. + +### Xcode Projects Note that even if you are otherwise using ANTLR from a binary distribution, you should compile the ANTLR Swift runtime from source, because the Swift @@ -37,24 +48,25 @@ Swift Package Manager does not currently support iOS, watchOS, or tvOS, so if you wish to use those platforms, you will need to alter the project build settings manually as appropriate. -### Download source code for ANTLR +#### Download source code for ANTLR ``` git clone https://github.com/antlr/antlr4 ``` -### Generate Xcode project for ANTLR runtime +#### Generate Xcode project for ANTLR runtime The `boot.py` script includes a wrapper around `swift package generate-xcodeproj`. Use this to generate `Antlr4.xcodeproj` for the ANTLR -Swift runtime. +Swift runtime. (using _swift package generate-xcodeproj_ is not recommended) +since the project is dependent on some parser files generated by _boot.py_. ``` cd antlr4/runtime/Swift python boot.py --gen-xcodeproj ``` -### Import ANTLR Swift runtime into your project +#### Import ANTLR Swift runtime into your project Open your own project in Xcode. @@ -73,13 +85,13 @@ will be able to see `Antlr4.xcodeproj` shown as a contained project. -### Edit the build settings if necessary +#### Edit the build settings if necessary Swift Package Manager currently does not support iOS, watchOS, or tvOS. If you wish to build for those platforms, you will need to alter the project build settings manually. -### Add generated parser and lexer to project +#### Add generated parser and lexer to project Make sure the parsers/lexers generated in __step 2__ are added to the project. To do this, you can @@ -92,7 +104,7 @@ are correct for your project. -### Add the ANTLR Swift runtime as a dependency +#### Add the ANTLR Swift runtime as a dependency Select your own project in Xcode and go to the Build Phases settings panel. Add the ANTLR runtime under __Target Dependencies__ and __Link Binary With @@ -100,6 +112,32 @@ Libraries__. -### Build your project +#### Build your project The runtime and generated grammar should now build correctly. + +### Swift Package Manager Projects + +Since we cannot have a separate repository for Swift target (see issue [#1774](https://github.com/antlr/antlr4/issues/1774)), +and Swift is currently not ABI stable. We currently support support SPM-based +projects by creating temporary local repository. + +For people using [Swift Package Manager](https://swift.org/package-manager/), +the __boot.py__ script supports generating local repository that can be used +as a dependency to your project. Simply run: + +``` +python boot.py --gen-spm-module +``` + +The prompt will show something like below: + + + +Put the SPM directive that contains the url to temporary repository to your +project's Package.swift. And run `swift build` in your project. + +The project is generated in your system's `/tmp/` directory, if you find it +inconvenient, consider copy that generated ANTLR repository to some place +that won't be cleaned automatically and update `url` parameter in your +`Package.swift` file. diff --git a/runtime/Swift/boot.py b/runtime/Swift/boot.py index b362224de..ff82c9e00 100755 --- a/runtime/Swift/boot.py +++ b/runtime/Swift/boot.py @@ -110,7 +110,7 @@ def get_argument_parser(): "Use this command if you want to include ANTLR4 as SPM dependency.", ) p.add_argument("--gen-xcodeproj", action="store_true", - help=" Generates an Xcode project for ANTLR4 Swift runtime. " + help=" Generates an Xcode project for ANTLR4 Swift runtime. " "This directive will generate all the required parsers for the project. " "Feel free to re-run whenever you updated the test grammar files.") p.add_argument("--test", @@ -147,7 +147,11 @@ def generate_spm_module(in_folder=TMP_FOLDER): call(["git", "tag", "{}.0.0".format(MAJOR_VERSION)]) antlr_says("Created local repository.") - antlr_says("Put .Package(url: \"{}\", majorVersion: {}) in Package.swift.".format(os.getcwd(), MAJOR_VERSION)) + antlr_says("(swift-tools-version:3.0) " + "Put .Package(url: \"{}\", majorVersion: {}) in Package.swift.".format(os.getcwd(), MAJOR_VERSION)) + antlr_says("(swift-tools-wersion:4.0) " + "Put .package(url: \"{}\", from: \"{}.0.0\") in Package.swift " + "and add \"Antlr4\" to target dependencies. ".format(os.getcwd(), MAJOR_VERSION)) def generate_xcodeproj():