Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Windows Support for v2.0 #25

Merged
merged 22 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions coinlib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.0-rc.8

Add windows build support

## 2.0.0-rc.7

- Add `CoinUnit` class to convert between amount strings and satoshis.
Expand Down
56 changes: 50 additions & 6 deletions coinlib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ dart pub add coinlib
```

If you are using the library for web, the library is ready to use. If you are
using the library on Linux or macOS then please see
["Building for Linux"](#building-for-linux) and
["Building for macOS"](#building-for-macos) below.

No script is included for building on Windows at the present time, however a
`secp256k1.dll` may be built into your `build/` directory separately.
using the library on Linux, macOS, or Windows, then please see
["Building for Linux"](#building-for-linux),
["Building for macOS"](#building-for-macos), or
["Building for Windows"](#building-for-windows) below.

The library can be imported via:

Expand Down Expand Up @@ -85,6 +83,52 @@ framework named `secp256k1.framework`.
To build the dynamic library, run `dart run coinlib:build_macos` which will
place the library under a `build` directory.

## Building for Windows

### Native Windows build

The Windows shared library can be built using `dart run coinlib:build_windows` in
the root directory of your package which will produce a shared library into
`build/libsecp256k1.dll`. This can also be run in the `coinlib` root directory
via `dart run bin/build_windows.dart`.

Windows builds use the Visual Studio 17 2022 generator. Earlier Visual Studio
toolchains may work by editing `bin/build_windows.dart`.

### Cross-compiling for Windows from Linux

Cross-compile a secp256k1 DLL for Windows on an Ubuntu 20.04 host with
`dart run coinlib:build_windows_crosscompile`. This can also be run in the
`coinlib` root directory via `dart run bin/build_windows_crosscompile.dart`.

### Cross-compiling for Windows using WSL

Builds on Windows can be accomplished using WSL2 (Windows Subsystem for Linux).
First, install the following packages to the WSL(2) host:

- `autoconf`
- `libtool`
- `build-essential`
- `git`
- `cmake`
- `mingw-w64`

as in:

```
apt-get update -y
apt-get install -y autoconf libtool build-essential git cmake mingw-w64
```

Then, cross-compile a secp256k1 DLL for Windows on an Ubuntu 20.04 WSL2 instance
on a Windows host with `dart run coinlib:build_wsl` or
`dart run bin/build_wsl.dart` in the `coinlib` root directory, or complete the
above
["Cross-compiling for Windows on Linux"](#cross-compiling-for-windows-from-linux)
after installing Docker or Podman in WSL. The build can also be completed
without installing Flutter to WSL by following
[bitcoin-core/secp256k1's "Cross compiling" guide](https://github.com/bitcoin-core/secp256k1?tab=readme-ov-file#cross-compiling).

## Development

This section is only relevant to developers of the library.
Expand Down
1 change: 0 additions & 1 deletion coinlib/bin/build_linux.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,4 @@ void main() async {
exit(1);
}


}
54 changes: 54 additions & 0 deletions coinlib/bin/build_windows.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import 'dart:io';

import 'util.dart';

/// Follows bitcoin-core/secp256k1's "Building on Windows" instructions.
///
/// Runnable in "Developer Command Prompt for VS 2022".

void main() async {

// Make temporary directory.
final workDir = Directory.current.path;
final tmpDir = createTmpDir();

// Clone bitcoin-core/secp256k1.
await execWithStdio(
"git",
["clone", "https://github.com/bitcoin-core/secp256k1", "$tmpDir/secp256k1"],
);
Directory.current = Directory("$tmpDir/secp256k1");
await execWithStdio(
"git",
["checkout", "346a053d4c442e08191f075c3932d03140579d47"],
);

// Build in tmpDir/secp256k1/build.
Directory("build").createSync();

// Configure cmake.
await execWithStdio("cmake", [
"-G",
"Visual Studio 17 2022",
"-A",
"x64",
"-S",
".",
"-B",
"build",
]);

// Build.
await execWithStdio("cmake", [
"--build",
"build",
"--config",
"RelWithDebInfo",
]);

// Copy the DLL to build/windows/x64/secp256k1.dll.
Directory("$workDir/build").createSync();
File("$tmpDir/secp256k1/build/src/RelWithDebInfo/secp256k1.dll")
.copySync("$workDir/build/secp256k1.dll");

}
46 changes: 46 additions & 0 deletions coinlib/bin/build_windows_crosscompile.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'dart:io';
import 'docker_util.dart';

/// Build a Windows DLL for secp256k1 using a Dockerfile string.

String dockerfile = r"""
FROM debian:bullseye

# Install dependenices.
RUN apt-get update -y \
&& apt-get install -y autoconf libtool build-essential git cmake gcc-mingw-w64

# Clone libsecp256k1 0.3.1 release.
RUN git clone https://github.com/bitcoin-core/secp256k1 \
&& cd secp256k1 \
&& git checkout 346a053d4c442e08191f075c3932d03140579d47 \
&& mkdir build

WORKDIR /secp256k1/build

# Build shared library for Windows.
RUN cmake .. -DCMAKE_TOOLCHAIN_FILE=../cmake/x86_64-w64-mingw32.toolchain.cmake
RUN make

# Build DLL and copy into output.
RUN make install
RUN mkdir output
RUN cp src/libsecp256k1.dll output/secp256k1.dll
""";

void main() async {

String cmd = await getDockerCmd();
print("Using $cmd to run dockerfile");

// Build secp256k1 and copy shared library to build directory
if (!await dockerBuild(
cmd,
dockerfile,
"coinlib_build_secp256k1_windows",
"output/secp256k1.dll",
)) {
exit(1);
}

}
44 changes: 44 additions & 0 deletions coinlib/bin/build_wsl.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import 'dart:io';

import 'util.dart';

/// Follows bitcoin-core/secp256k1's "Cross compiling" instructions.
///
/// Runnable in WSL. Install the dependencies listed in the README:
/// ```
/// apt-get install -y autoconf libtool build-essential git cmake mingw-w64
/// ```

void main() async {
// Make temporary directory.
final workDir = Directory.current.path;
final tmpDir = createTmpDir();

// Clone bitcoin-core/secp256k1.
await execWithStdio(
"git",
["clone", "https://github.com/bitcoin-core/secp256k1", "$tmpDir/secp256k1"],
);
Directory.current = Directory("$tmpDir/secp256k1");
await execWithStdio(
"git",
["checkout", "346a053d4c442e08191f075c3932d03140579d47"],
);

// Build in tmpDir/secp256k1/lib.
Directory("lib").createSync();
Directory.current = Directory("lib");

// Run cmake with the provided toolchain file.
await execWithStdio("cmake", [
"..",
"-DCMAKE_TOOLCHAIN_FILE=../cmake/x86_64-w64-mingw32.toolchain.cmake",
]);

// Build the project using "make".
await execWithStdio("make", []);

// Copy the DLL to build/libsecp256k1.dll.
Directory("$workDir/build").createSync();
File("src/libsecp256k1.dll").copySync("$workDir/build/secp256k1.dll");
}
1 change: 0 additions & 1 deletion coinlib/lib/src/secp256k1/secp256k1_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ String _libraryPath() {
localLib = "lib$_name.dylib";
flutterLib = "$_name.framework/$_name";
} else if (Platform.isWindows) {
// Not provided yet, so should fail to load unless added outside of library
flutterLib = localLib = "$_name.dll";
} else {
throw UnsupportedError('Unknown platform: ${Platform.operatingSystem}');
Expand Down
2 changes: 1 addition & 1 deletion coinlib/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: coinlib
description:
A straight-forward, modular library for Peercoin and other Satoshi-based UTXO
blockchains
version: 2.0.0-rc.7
version: 2.0.0-rc.8
repository: https://github.com/peercoin/coinlib

environment:
Expand Down
32 changes: 19 additions & 13 deletions coinlib_flutter/.metadata
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled.
# This file should be version controlled and should not be manually edited.

version:
revision: 9944297138845a94256f1cf37beb88ff9a8e811a
channel: stable
revision: "2524052335ec76bb03e04ede244b071f1b86d190"
channel: "[user-branch]"

project_type: plugin_ffi

# Tracks metadata for the flutter migrate command
migration:
platforms:
- platform: root
create_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
base_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
create_revision: 2524052335ec76bb03e04ede244b071f1b86d190
base_revision: 2524052335ec76bb03e04ede244b071f1b86d190
- platform: android
create_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
base_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
create_revision: 2524052335ec76bb03e04ede244b071f1b86d190
base_revision: 2524052335ec76bb03e04ede244b071f1b86d190
- platform: ios
create_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
base_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
create_revision: 2524052335ec76bb03e04ede244b071f1b86d190
base_revision: 2524052335ec76bb03e04ede244b071f1b86d190
- platform: linux
create_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
base_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
create_revision: 2524052335ec76bb03e04ede244b071f1b86d190
base_revision: 2524052335ec76bb03e04ede244b071f1b86d190
- platform: macos
create_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
base_revision: 9944297138845a94256f1cf37beb88ff9a8e811a
create_revision: 2524052335ec76bb03e04ede244b071f1b86d190
base_revision: 2524052335ec76bb03e04ede244b071f1b86d190
- platform: web
create_revision: 2524052335ec76bb03e04ede244b071f1b86d190
base_revision: 2524052335ec76bb03e04ede244b071f1b86d190
- platform: windows
create_revision: 2524052335ec76bb03e04ede244b071f1b86d190
base_revision: 2524052335ec76bb03e04ede244b071f1b86d190

# User provided section

Expand Down
4 changes: 4 additions & 0 deletions coinlib_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.0-rc.8

Add windows build support

## 2.0.0-rc.7

Update to underlying coinlib 2.0.0-rc.7
Expand Down
2 changes: 1 addition & 1 deletion coinlib_flutter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ An example app is provided in `example/` that demonstrates use of the loader
widget. Beyond this, the [coinlib](https://pub.dev/packages/coinlib) library
documentation can be followed.

Android, iOS, Linux, macOS and Web are supported.
Android, iOS, Linux, macOS, web, and Windows are supported. If you are using the library for web, Android, or iOS, the library is ready to use. For Windows, run `dart run coinlib:build_windows` to build the library. See [coinlib's documentation](https://pub.dev/packages/coinlib) for more detailed instructions on and options for building the native library.
8 changes: 4 additions & 4 deletions coinlib_flutter/example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ packages:
dependency: transitive
description:
name: coinlib
sha256: cd94f17589db95a2be05648ad2bef8f891e08a193db0c569fe1e32a98b836290
sha256: c05effeb5cbc9edac2afd1378ea3d463a12db455923519a748527d3caf1f2555
url: "https://pub.dev"
source: hosted
version: "2.0.0-rc.6"
version: "2.0.0-rc.8"
coinlib_flutter:
dependency: "direct main"
description:
name: coinlib_flutter
sha256: fbb66384c38ce74ff197d5e1d7985bc22d98996b2dcd0d7787735db60ab1a193
sha256: ee49acc206f233373c892785b639b7b7c926233f27b74dc4acd8849b8504be2f
url: "https://pub.dev"
source: hosted
version: "2.0.0-rc.6"
version: "2.0.0-rc.8"
collection:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion coinlib_flutter/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ environment:
dependencies:
flutter:
sdk: flutter
coinlib_flutter: ^2.0.0-rc.7
coinlib_flutter: ^2.0.0-rc.8
cupertino_icons: ^1.0.2

dev_dependencies:
Expand Down
17 changes: 17 additions & 0 deletions coinlib_flutter/example/windows/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
flutter/ephemeral/

# Visual Studio user-specific files.
*.suo
*.user
*.userosscache
*.sln.docstates

# Visual Studio build-related files.
x64/
x86/

# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
Loading