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

Cross-build for Scala Native #373

Merged
merged 7 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
50 changes: 47 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
os: [ubuntu-latest]
scala: [2.11.12, 2.12.15, 2.13.8, 3.1.2]
java: [temurin@8]
project: [rootJS, rootJVM]
project: [rootJS, rootJVM, rootNative]
runs-on: ${{ matrix.os }}
steps:
- name: Checkout current branch (full)
Expand Down Expand Up @@ -77,6 +77,10 @@ jobs:
if: matrix.project == 'rootJS'
run: sbt 'project ${{ matrix.project }}' '++${{ matrix.scala }}' Test/scalaJSLinkerResult

- name: nativeLink
if: matrix.project == 'rootNative'
run: sbt 'project ${{ matrix.project }}' '++${{ matrix.scala }}' Test/nativeLink

- name: Test
run: sbt 'project ${{ matrix.project }}' '++${{ matrix.scala }}' test

Expand All @@ -90,11 +94,11 @@ jobs:

- name: Make target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: mkdir -p target .js/target core/js/target core/jvm/target .jvm/target .native/target benchmark/target project/target
run: mkdir -p target .js/target core/native/target core/js/target core/jvm/target .jvm/target .native/target benchmark/target project/target

- name: Compress target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
run: tar cf targets.tar target .js/target core/js/target core/jvm/target .jvm/target .native/target benchmark/target project/target
run: tar cf targets.tar target .js/target core/native/target core/js/target core/jvm/target .jvm/target .native/target benchmark/target project/target

- name: Upload target directories
if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main')
Expand Down Expand Up @@ -167,6 +171,16 @@ jobs:
tar xf targets.tar
rm targets.tar

- name: Download target directories (2.11.12, rootNative)
uses: actions/download-artifact@v2
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-2.11.12-rootNative

- name: Inflate target directories (2.11.12, rootNative)
run: |
tar xf targets.tar
rm targets.tar

- name: Download target directories (2.12.15, rootJS)
uses: actions/download-artifact@v2
with:
Expand All @@ -187,6 +201,16 @@ jobs:
tar xf targets.tar
rm targets.tar

- name: Download target directories (2.12.15, rootNative)
uses: actions/download-artifact@v2
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-2.12.15-rootNative

- name: Inflate target directories (2.12.15, rootNative)
run: |
tar xf targets.tar
rm targets.tar

- name: Download target directories (2.13.8, rootJS)
uses: actions/download-artifact@v2
with:
Expand All @@ -207,6 +231,16 @@ jobs:
tar xf targets.tar
rm targets.tar

- name: Download target directories (2.13.8, rootNative)
uses: actions/download-artifact@v2
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-2.13.8-rootNative

- name: Inflate target directories (2.13.8, rootNative)
run: |
tar xf targets.tar
rm targets.tar

- name: Download target directories (3.1.2, rootJS)
uses: actions/download-artifact@v2
with:
Expand All @@ -227,6 +261,16 @@ jobs:
tar xf targets.tar
rm targets.tar

- name: Download target directories (3.1.2, rootNative)
uses: actions/download-artifact@v2
with:
name: target-${{ matrix.os }}-${{ matrix.java }}-3.1.2-rootNative

- name: Inflate target directories (3.1.2, rootNative)
run: |
tar xf targets.tar
rm targets.tar

- name: Import signing key
if: env.PGP_SECRET != '' && env.PGP_PASSPHRASE == ''
run: echo $PGP_SECRET | base64 -di | gpg --import
Expand Down
13 changes: 10 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,15 @@ ThisBuild / mimaBinaryIssueFilters ++= Seq(
ProblemFilters.exclude[IncompatibleMethTypeProblem]("scodec.bits.BitVector.reduceBalanced"),
ProblemFilters.exclude[MissingClassProblem]("scodec.bits.BuildInfo"),
ProblemFilters.exclude[MissingClassProblem]("scodec.bits.BuildInfo$"),
ProblemFilters.exclude[DirectMissingMethodProblem]("scodec.bits.crc.vectorTable")
ProblemFilters.exclude[DirectMissingMethodProblem]("scodec.bits.crc.vectorTable"),
ProblemFilters.exclude[IncompatibleMethTypeProblem](
"scodec.bits.ByteVector#ByteVectorInputStream#CustomAtomicInteger.getAndUpdate_"
)
)

lazy val root = tlCrossRootProject.aggregate(core, benchmark)

lazy val core = crossProject(JVMPlatform, JSPlatform)
lazy val core = crossProject(JVMPlatform, JSPlatform, NativePlatform)
.in(file("core"))
.settings(
name := "scodec-bits",
Expand All @@ -82,7 +85,7 @@ lazy val core = crossProject(JVMPlatform, JSPlatform)
(base / "NOTICE") +: (base / "LICENSE") +: ((base / "licenses") * "LICENSE_*").get
},
scalacOptions := scalacOptions.value.filterNot(_ == "-source:3.0-migration"),
libraryDependencies += "org.scalameta" %%% "munit-scalacheck" % "0.7.29" % "test"
libraryDependencies += "org.scalameta" %%% "munit-scalacheck" % "1.0.0-M4" % "test"
)

lazy val coreJVM = core.jvm
Expand Down Expand Up @@ -133,6 +136,10 @@ lazy val coreJS = core.js.settings(
)
)

lazy val coreNative = core.native.settings(
tlVersionIntroduced ++= List("2.12", "2.13", "3").map(_ -> "1.1.32").toMap
)

lazy val benchmark: Project = project
.in(file("benchmark"))
.dependsOn(coreJVM)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2013, Scodec
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package scodec.bits

private[bits] trait BitVectorCompanionCrossPlatform extends BitVectorPlatform {
self: BitVector.type =>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2013, Scodec
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package scodec.bits

private[bits] trait BitVectorCrossPlatform { self: BitVector => }
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2013, Scodec
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package scodec.bits

import scala.scalanative.unsafe.Ptr

private[bits] trait ByteVectorCompanionCrossPlatform extends ByteVectorPlatform {
self: ByteVector.type =>

def view(ptr: Ptr[Byte], size: Long): ByteVector =
ByteVector.viewAt(ptr(_), size) // TODO pending ptr-backed ByteBuffers in Native stdlib ...

def fromPtr(ptr: Ptr[Byte], size: Long): ByteVector =
view(ptr, size).copy
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2013, Scodec
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package scodec.bits

import scala.scalanative.unsafe.Ptr
import scala.scalanative.unsafe.Zone
import scala.scalanative.unsafe.alloc

private[bits] trait ByteVectorCrossPlatform { self: ByteVector =>
import ByteVector._

def copyToPtr(dest: Ptr[Byte], start: Long): Unit =
copyToPtr(dest, start, 0, size)

def copyToPtr(dest: Ptr[Byte], start: Long, offset: Long, size: Long): Unit = {
var i = 0L
while (i < size) {
dest(start + i) = self(offset + i)
i += 1
}
}

def toPtr(implicit zone: Zone): Ptr[Byte] = {
val dest = alloc[Byte](size)
copyToPtr(dest, 0)
dest
}
}
82 changes: 82 additions & 0 deletions core/native/src/test/scala/scodec/bits/ByteVectorNativeTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2013, Scodec
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package scodec.bits

import scala.scalanative.unsafe.Ptr
import scala.scalanative.unsafe.Zone
import scala.scalanative.unsafe.alloc

class ByteVectorNativeTest extends BitsSuite {
private def setDEADBEEF(ptr: Ptr[Byte]): Unit = {
ptr(0) = 0xde.toByte
ptr(1) = 0xad.toByte
ptr(2) = 0xbe.toByte
ptr(3) = 0xef.toByte
}

test("view(Ptr[Byte])") {
Zone { implicit z =>
val ptr = alloc[Byte](4)
setDEADBEEF(ptr)
val byteVector: ByteVector = ByteVector.view(ptr, 4)
assert(byteVector === hex"deadbeef")
ptr(3) = 0xee.toByte
assert(byteVector === hex"deadbeee")
}
}

test("fromPtr") {
Zone { implicit z =>
val ptr = alloc[Byte](4)
setDEADBEEF(ptr)
val byteVector: ByteVector = ByteVector.fromPtr(ptr, 4)
assert(byteVector === hex"deadbeef")
ptr(3) = 0xee.toByte
assert(byteVector === hex"deadbeef")
}
}

test("copyToPtr") {
Zone { implicit z =>
val ptr = alloc[Byte](6)
ptr(0) = 0xaa.toByte
ptr(5) = 0xff.toByte
hex"bbccdeadbeefee".copyToPtr(ptr, 1, 2, 4)
assert(ByteVector.view(ptr, 6) === hex"aadeadbeefff")
}
}

test("toPtr") {
Zone { implicit z =>
assert(ByteVector.view(hex"deadbeef".toPtr, 4) === hex"deadbeef")
}
}
}
Loading