• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

gradle/native-samples: Samples of modern build automation for native languages w ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

gradle/native-samples

开源软件地址(OpenSource Url):

https://github.com/gradle/native-samples

开源编程语言(OpenSource Language):

Java 42.1%

开源软件介绍(OpenSource Introduction):

Swift and C++ Sample Projects

This repository holds several sample Gradle builds which demonstrate how to use Gradle to build Swift/C++ libraries and applications as introduced in our blog post.

Each sample build is listed below with a bit of information related to the features of Gradle that are demonstrated in that build. Each sample is functionally the same for both Swift and C++ languages.

Each C++ sample works on macOS, Linux and Windows with GCC, Clang and Visual C++.

Each Swift sample works on macOS and Linux, with Swift 3 and later.

Deeper build insights with Build Scans

You can generate build-scans with these samples by running Gradle with --scan. At the end of the build, you will be prompted to upload build data to scans.gradle.com.

As an example of adding more data to a build scan, you can also run any sample with -I ../../build-scan/buildScanUserData.gradle in combination with --scan. This will add custom values that describe what is being built like these.

Contributing to these samples

If you want to contribute an improvement to the samples, please refer to the samples-dev subproject.

Suggesting new samples

If you have a use case that isn't covered by an existing sample, open an issue for gradle-native. Please describe what you're trying to accomplish so we can help you find a solution.

Visual Studio support

All of the C++ samples have Visual Studio support, added by applying the visual-studio plugin. To open a sample build in Visual Studio:

> cd <sample-dir>
> ./gradlew openVisualStudio

Xcode support

All of the samples have Xcode support, added by applying the xcode plugin. To open a sample build in Xcode:

> cd <sample-dir>
> ./gradlew openXcode

XCTest support

All Swift samples demonstrate XCTest support in Gradle. As a user, you can either interact with the test code as you would usually do through Xcode or you can run the test directly from the command line:

> cd <sample-dir>
> ./gradlew test

Google Test support

The C++ sample simple-library demonstrates some basic Google test support in Gradle. This is currently not as refined as the XCTest support.

To run the tests from the command line:

> cd <sample-dir>
> ./gradlew test

Incremental Swift compilation

The swiftc compiler has a built-in incremental compilation feature that tries to reduce the number of .swift files that need to be recompiled on each build by analyzing the dependencies between all files.

Gradle enables Swift incremental compilation by default, so no extra configuration is required to take advantage of this feature with your Swift projects.

Debug and release variants

The Swift and C++ plugins add a 'debug' and 'release' variant for each library or application. By default, the assemble task will build the debug variant only.

You can also use the assembleDebug and assembleRelease tasks to build a specific variant, or both variants.

At this stage, there are no convenience tasks to build all variants of a library or application.

Publishing binaries to a Maven repository

Some of the C++ samples are configured to publish binaries to a local Maven repository. For these samples you can run:

> cd <sample-dir>
> ./gradle publish
> tree ../repo/

This will build and publish the debug and release binaries. The binaries are published to a repository in the cpp/repo directory.

Simple application (application)

This sample shows how a simple Swift or C++ application can be built with Gradle. The application has no dependencies and the build has minimal configuration.

Although there is currently no out-of-the-box support for building applications and libraries from C, there is also a sample build that shows how the C++ support can be configured to build a C application.

C++

To build and run the application:

> cd cpp/application
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/app
Hello, World!

Swift

To build and run the application:

> cd swift/application
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/App
Hello, World!

C

To build and run the application:

> cd c/application
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/app
Hello, World!

Simple Library (simple-library)

This sample shows how a Swift or C++ library can be built with Gradle. The library has no dependencies. The build is configured to add unit tests. The C++ sample also adds binary publishing to a Maven repository.

C++

To build the library:

> cd cpp/simple-library
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> find build/lib/main/debug
build/lib/main/debug/liblist.dylib

To run the unit tests for the library:

> ./gradlew test

BUILD SUCCESSFUL in 1s

Swift

To build the library:

> cd swift/simple-library
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> find build/lib/main/debug
build/lib/main/debug/libList.dylib

To run the unit tests for the library:

> ./gradlew test
> open build/reports/tests/xcTest/index.html

Application with library dependencies in a single build (transitive-dependencies)

This sample shows how a C++ or Swift application and several libraries can be built with Gradle and linked together. The sample is structured as a multi-project build, with the application and each library as separate projects in this build. Dependencies are added using project dependencies.

In this sample, the application and libraries all use the same implementation language. Mixing C++ and Swift is shown in another sample below.

C++

To build and run the application:

> cd cpp/transitive-dependencies
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./app/build/install/main/debug/app
Hello, World!

The build script also demonstrates how to configure convenience tasks like assembleDebuggable, which will assemble all "debuggable" binaries.

> ./gradlew assembleDebuggable

BUILD SUCCESSFUL in 1s

Swift

To build and run the application:

> cd swift/transitive-dependencies
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./app/build/install/main/debug/App
Hello, World!

Application with Windows Resources (windows-resources)

This sample shows how Gradle is able to compile Windows Resources (rc) files and link them into a native binary. This sample applies the 'cpp-application' plugin. This sample requires you have VisualCpp toolchain installed

C++

To build and run the application: (Note the application only runs and build on Windows)

> cd cpp/windows-resources
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/exe/main/debug/app.exe
Hello, World!

Application with precompiled headers (precompiled-headers)

This sample shows how Gradle is able to compile code using precompiled headers. This sample applies the 'cpp-application' plugin.

C++

To build and run the application:

> cd cpp/precompiled-headers
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/exe/main/debug/app
Hello, World!

Application with library dependencies in a composite build (composite-build)

This sample shows how several otherwise independent Swift or C++ libraries can be built together with Gradle. The sample is structured as separate builds for each of the libraries and a composite build that includes these.

C++

To build and run the application:

> cd cpp/composite-build
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/app
Hello, World!

Swift

To build and run the application:

> cd swift/composite-build
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/App
Hello, World!

Application with prebuilt library dependencies in a Maven repository (binary-dependencies)

This sample shows how to publish C++ libraries to a Maven repository and use them from another build. This is currently not supported for Swift.

C++

To use the sample, first publish a library to the repository using the simple-library build:

> cd cpp/binary-dependencies
> ./gradlew -p ../simple-library publish

BUILD SUCCESSFUL in 1s

You can find the repository in the cpp/repo directory.

Next, build the application that uses the library from this repository.

> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/app
Hello, World!

The build is also configured to download the Google test binaries from a Maven repository to build and run the unit tests:

> ./gradlew test

BUILD SUCCESSFUL in 1s

Application with prebuilt library dependencies (prebuilt-binaries)

This sample shows how to use pre-built binaries that are already available on the local machine. Currently, Gradle does not offer a convenient way to do this but it is possible to configure Gradle to use these binaries.

C++

To use the sample, first create the binaries using the simple-library build:

> cd cpp/prebuilt-binaries
> ./gradlew -p ../simple-library assembleDebug assembleRelease

BUILD SUCCESSFUL in 1s

Next, build and run the application that uses these binaries:

> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/app
Hello, World!

Swift

To use the sample, first create the binaries using the simple-library build:

> cd swift/prebuilt-binaries
> ./gradlew -p ../simple-library assembleDebug assembleRelease

BUILD SUCCESSFUL in 1s

Next, build and run the application that uses these binaries:

> ./gradlew assemble

BUILD SUCCESSFUL in 0s

> ./build/install/main/debug/App
Hello, World!

Application with Swift package manager conventions (swift-package-manager)

This sample shows how to configure Gradle to use a source layout that is different to its conventions. In this case, the sample uses the typical layout for a Swift Package Manager package. It contains an application and a single library. The source files for the application and libraries are all under a single Sources directory.

This sample also includes a Swift Package Manager build file, so the same source can be built using Swift Package Manager

C++

> cd cpp/swift-package-manager
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/app/install/main/debug/app
Hello, World!

Swift

> cd swift/swift-package-manager
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/app/install/main/debug/App
Hello, World!

Source generation (source-generation)

This sample demonstrates using a task to generate source code before building a Swift or C++ application.

C++

> cd cpp/source-generation
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/app
Hello, World!

Generated sources will be under build/generated.

Swift

> cd swift/source-generation
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/App
Hello, World!

Generated sources will be under build/generated.

Application with source library dependencies (source-dependencies)

This sample demonstrates using external source dependencies to build Swift and C++ applications that require two libraries. The source for the libraries are hosted in separate Git repositories and declared as 'source dependencies' of the application. When Gradle builds the application, it first checks out a revision of the library source and uses this to build the binaries for the library.

The Git repositories to use are declared in the build's settings.gradle and then the libraries are referenced in the same way as binary dependencies in the build files.

Swift

To use this sample, build and run the application:

> cd swift/source-dependencies
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/App
World!

This will clone version 1.0 of the Git repository at https://github.com/gradle/native-samples-swift-library and build the library binaries.

You can see the application's output is incorrect. The build is configured to use version 1.0 of the utilities library from this repository and this version contains a bug. Let's fix this.

Version 1.1 of the library contains a bug fix. Update the application to use the new version:

> cd ../..
> edit build.gradle # change dependency on org.gradle.swift-samples:utilities:1.0 to org.gradle.swift-samples:utilities:1.1
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/App
Hello, World!

Dynamic dependencies are also supported, so you could also use 1.+, [1.1,2.0] or latest.release. Gradle matches the tags of the Git repository to determine which Git revision to use. Branches are also supported, but use a different syntax. See the following sample.

C++

To use this sample, build and run the application:

> cd cpp/source-dependencies
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/app
World!

This will clone version 1.0 of the Git repository at https://github.com/gradle/native-samples-cpp-library and build the library binaries.

The application output is incorrect because of a bug in the utilities library.

Update the application to use a new version that contains a fix:

> cd ../..
> edit build.gradle # change dependency on org.gradle.cpp-samples:utilities:1.0 to org.gradle.cpp-samples:utilities:1.1
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/app
Hello, World!

Application with dependency on upstream branch (dependency-on-upstream-branch)

This sample shows how a source dependency on a particular branch can be used.

Swift

To use this sample, create the Git repositories containing the libraries:

> cd swift/dependency-on-upstream-branch/app
> ./gradlew -p ../../.. generateRepos

Now you can build and run the application:

> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/App
World!

You can see the application's output is incorrect. The build is configured to use the most recent changes from the 'release' branch of the utilities library and this branch contains a bug. Let's fix this.

Edit the source of the utilities library to fix the bug:

> cd repos/utilities-library
> git checkout release
> edit src/main/swift/Util.swift # follow the instructions to fix the bug in function join()
> git commit -a -m 'fixed bug'

There's no need to create a tag, as Gradle will take care of checking out the new branch tip.

Now build and run the application:

> cd ../..
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/App
Hello, World!

C++

To use this sample, create the Git repositories containing the libraries:

> cd cpp/dependency-on-upstream-branch/app
> ./gradlew -p ../../.. generateRepos

Build and run the application:

> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/app
World!

Edit the source of the utilities library to fix the bug:

> cd repos/utilities-library
> git checkout release
> edit src/main/cpp/join.cpp # follow the instructions to fix the bug in function join()
> git commit -a -m 'fixed bug'

Now build the application:

> cd ../..
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./build/install/main/debug/app
Hello, World!

Application with static library dependencies (static-library)

This sample demonstrates building and using static libraries.

C++

> cd cpp/static-library
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./app/build/install/main/debug/app
Hello, World!

Swift

> cd swift/static-library
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> ./app/build/install/main/debug/App
Hello, World!

Application with operating system specific library dependencies (operating-system-specific-dependencies)

This sample demonstrates an application that has dependencies on different libraries for each operating system. Currently, there are no conveniences for using libraries that are installed on the build machine.

Swift

> cd swift/operating-system-specific-dependencies
> ./gradlew assemble

BUILD SUCCESSFUL in 1s

> app/build/install/main/debug/App
Hello, World!

The application selects the 'MacOsConsole' library that prints the output in blue when building on macOS. On Linux, it selects the 'LinuxConsole' library that prints the output in green. Each console library is configured to only build on specific operating systems.

C++

> cd cpp/operating-system-specific 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap