Skip to content

Commit fbeaa1a

Browse files
authored
Merge pull request #3 from kotlin-hands-on/new-tutorial-project
update: new tutorial project for the curl documentation tutorial
2 parents 671b07e + b87f813 commit fbeaa1a

File tree

10 files changed

+106
-104
lines changed

10 files changed

+106
-104
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66

7-
# Introduction to Kotlin/Native Hands-On Lab
7+
# Kotlin/Native tutorial: C interop and libcurl
88

99

10-
This repository is the code corresponding to the hands-on lab [Introduction to Kotlin/Native Hands-On Lab](https://play.kotlinlang.org/hands-on/Introduction%20to%20Kotlin%20Native/01_Introduction).
10+
This repository is the resulting code corresponding to the [Creating an app using C Interop and libcurl](https://kotlinlang.org/docs/curl.html) tutorial.

build.gradle

+25-28
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,41 @@
11
plugins {
2-
id 'kotlin-multiplatform' version '1.5.30'
2+
id 'org.jetbrains.kotlin.multiplatform' version '1.5.31'
33
}
4+
5+
group = 'me.user'
6+
version = '1.0-SNAPSHOT'
7+
48
repositories {
59
mavenCentral()
610
}
11+
712
kotlin {
8-
// For ARM, should be changed to iosArm32 or iosArm64
9-
// For Linux, should be changed to e.g. linuxX64
10-
// For MacOS, should be changed to e.g. macosX64
11-
// For Windows, should be changed to e.g. mingwX64
12-
macosX64("macos") {
13-
compilations.main {
14-
cinterops {
15-
libcurl /*{
16-
defFile project.file("libcurl.def")
17-
packageName 'com.jetbrains.handson.http'
18-
compilerOpts '-I/'
19-
includeDirs.allHeaders("")
20-
}*/
21-
}
22-
}
13+
def hostOs = System.getProperty("os.name")
14+
def isMingwX64 = hostOs.startsWith("Windows")
15+
def nativeTarget
16+
if (hostOs == "Mac OS X") nativeTarget = macosX64('native')
17+
else if (hostOs == "Linux") nativeTarget = linuxX64("native")
18+
else if (isMingwX64) nativeTarget = mingwX64("native")
19+
else throw new FileNotFoundException("Host OS is not supported in Kotlin/Native.")
20+
21+
nativeTarget.with {
22+
compilations.main { // NL
23+
cinterops { // NL
24+
libcurl // NL
25+
} // NL
26+
} // NL
2327
binaries {
2428
executable {
25-
// Change to specify fully qualified name of your application's entry point:
26-
entryPoint = 'com.jetbrains.handson.http.main'
27-
// Specify command-line arguments, if necessary:
28-
runTask?.args('')
29+
entryPoint = 'main'
2930
}
3031
}
3132
}
3233
sourceSets {
33-
// Note: To enable common source sets please comment out 'kotlin.import.noCommonSourceSets' property
34-
// in gradle.properties file and re-import your project in IDE.
35-
macosMain {
34+
nativeMain {
35+
3636
}
37-
macosTest {
37+
nativeTest {
38+
3839
}
3940
}
4041
}
41-
42-
// Use the following Gradle tasks to run your application:
43-
// :runReleaseExecutableMacos - without debug symbols
44-
// :runDebugExecutableMacos - with debug symbols

gradle.properties

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
kotlin.code.style=official
2-
kotlin.import.noCommonSourceSets=true
2+
kotlin.mpp.enableGranularSourceSetsMetadata=true
3+
kotlin.native.enableDependencyPropagation=false

gradle/wrapper/gradle-wrapper.jar

3.29 KB
Binary file not shown.

gradlew

+34-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
#!/usr/bin/env sh
22

3+
#
4+
# Copyright 2015 the original author or authors.
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# https://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
319
##############################################################################
420
##
521
## Gradle start up script for UN*X
@@ -28,7 +44,7 @@ APP_NAME="Gradle"
2844
APP_BASE_NAME=`basename "$0"`
2945

3046
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31-
DEFAULT_JVM_OPTS=""
47+
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
3248

3349
# Use the maximum available, or set MAX_FD != -1 to use that value.
3450
MAX_FD="maximum"
@@ -56,7 +72,7 @@ case "`uname`" in
5672
Darwin* )
5773
darwin=true
5874
;;
59-
MINGW* )
75+
MSYS* | MINGW* )
6076
msys=true
6177
;;
6278
NONSTOP* )
@@ -66,6 +82,7 @@ esac
6682

6783
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
6884

85+
6986
# Determine the Java command to use to start the JVM.
7087
if [ -n "$JAVA_HOME" ] ; then
7188
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
@@ -109,10 +126,11 @@ if $darwin; then
109126
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110127
fi
111128

112-
# For Cygwin, switch paths to Windows format before running java
113-
if $cygwin ; then
129+
# For Cygwin or MSYS, switch paths to Windows format before running java
130+
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
114131
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115132
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133+
116134
JAVACMD=`cygpath --unix "$JAVACMD"`
117135

118136
# We build the pattern for arguments to be converted via cygpath
@@ -138,19 +156,19 @@ if $cygwin ; then
138156
else
139157
eval `echo args$i`="\"$arg\""
140158
fi
141-
i=$((i+1))
159+
i=`expr $i + 1`
142160
done
143161
case $i in
144-
(0) set -- ;;
145-
(1) set -- "$args0" ;;
146-
(2) set -- "$args0" "$args1" ;;
147-
(3) set -- "$args0" "$args1" "$args2" ;;
148-
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149-
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150-
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151-
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152-
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153-
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
162+
0) set -- ;;
163+
1) set -- "$args0" ;;
164+
2) set -- "$args0" "$args1" ;;
165+
3) set -- "$args0" "$args1" "$args2" ;;
166+
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167+
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168+
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169+
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170+
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171+
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154172
esac
155173
fi
156174

@@ -159,14 +177,9 @@ save () {
159177
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160178
echo " "
161179
}
162-
APP_ARGS=$(save "$@")
180+
APP_ARGS=`save "$@"`
163181

164182
# Collect all arguments for the java command, following the shell quoting and substitution rules
165183
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166184

167-
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168-
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169-
cd "$(dirname "$0")"
170-
fi
171-
172185
exec "$JAVACMD" "$@"

gradlew.bat

+24-19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
@rem
2+
@rem Copyright 2015 the original author or authors.
3+
@rem
4+
@rem Licensed under the Apache License, Version 2.0 (the "License");
5+
@rem you may not use this file except in compliance with the License.
6+
@rem You may obtain a copy of the License at
7+
@rem
8+
@rem https://www.apache.org/licenses/LICENSE-2.0
9+
@rem
10+
@rem Unless required by applicable law or agreed to in writing, software
11+
@rem distributed under the License is distributed on an "AS IS" BASIS,
12+
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
@rem See the License for the specific language governing permissions and
14+
@rem limitations under the License.
15+
@rem
16+
117
@if "%DEBUG%" == "" @echo off
218
@rem ##########################################################################
319
@rem
@@ -13,15 +29,18 @@ if "%DIRNAME%" == "" set DIRNAME=.
1329
set APP_BASE_NAME=%~n0
1430
set APP_HOME=%DIRNAME%
1531

32+
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
33+
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34+
1635
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17-
set DEFAULT_JVM_OPTS=
36+
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
1837

1938
@rem Find java.exe
2039
if defined JAVA_HOME goto findJavaFromJavaHome
2140

2241
set JAVA_EXE=java.exe
2342
%JAVA_EXE% -version >NUL 2>&1
24-
if "%ERRORLEVEL%" == "0" goto init
43+
if "%ERRORLEVEL%" == "0" goto execute
2544

2645
echo.
2746
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
@@ -35,7 +54,7 @@ goto fail
3554
set JAVA_HOME=%JAVA_HOME:"=%
3655
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
3756

38-
if exist "%JAVA_EXE%" goto init
57+
if exist "%JAVA_EXE%" goto execute
3958

4059
echo.
4160
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
@@ -45,28 +64,14 @@ echo location of your Java installation.
4564

4665
goto fail
4766

48-
:init
49-
@rem Get command-line arguments, handling Windows variants
50-
51-
if not "%OS%" == "Windows_NT" goto win9xME_args
52-
53-
:win9xME_args
54-
@rem Slurp the command line arguments.
55-
set CMD_LINE_ARGS=
56-
set _SKIP=2
57-
58-
:win9xME_args_slurp
59-
if "x%~1" == "x" goto execute
60-
61-
set CMD_LINE_ARGS=%*
62-
6367
:execute
6468
@rem Setup the command line
6569

6670
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
6771

72+
6873
@rem Execute Gradle
69-
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
74+
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
7075

7176
:end
7277
@rem End local scope for the variables with windows NT shell

settings.gradle

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
pluginManagement {
2-
resolutionStrategy {
3-
eachPlugin {
4-
if (requested.id.id == "kotlin-multiplatform") {
5-
useModule("org.jetbrains.kotlin:kotlin-gradle-plugin:${requested.version}")
6-
}
7-
}
8-
}
9-
}
1+
102
rootProject.name = 'kotlin-hands-on-intro-kotlin-native'
113

src/macosMain/kotlin/com.jetbrains.handson.http/Main.kt

-22
This file was deleted.
+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
headers = curl/curl.h
22
headerFilter = curl/*
3+
4+
compilerOpts.linux = -I/usr/include -I/usr/include/x86_64-linux-gnu
35
linkerOpts.osx = -L/opt/local/lib -L/usr/local/opt/curl/lib -lcurl
4-
linkerOpts.linux = -L/usr/lib64 -L/usr/lib/x86_64-linux-gnu -lcurl
5-
linkerOpts.mingw = -lcurl
6+
linkerOpts.linux = -L/usr/lib/x86_64-linux-gnu -lcurl

src/nativeMain/kotlin/Main.kt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import kotlinx.cinterop.*
2+
import libcurl.*
3+
4+
fun main(args: Array<String>) {
5+
val curl = curl_easy_init()
6+
if (curl != null) {
7+
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com")
8+
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L)
9+
val res = curl_easy_perform(curl)
10+
if (res != CURLE_OK) {
11+
println("curl_easy_perform() failed ${curl_easy_strerror(res)?.toKString()}")
12+
}
13+
curl_easy_cleanup(curl)
14+
}
15+
}

0 commit comments

Comments
 (0)