Git Product home page Git Product logo

cphconsoleapp's Introduction

CPHConsoleApp

Cross Platform Haskell Console Application that outputs Hello World on iOS, Android and Desktop. This is merely a proof-of-concept, showing how to target several platforms from a single Haskell codebase.

Prerequisites

For all platforms:

  • GHC (tested with 7.10.2)
  • Cabal (tested with 1.22.6)

For iOS:

  • Machine with OSX installed (tested on El Capitan)
  • Xcode (tested with 7.0.1)

For Android:

  • Docker (1.6 or greater is required)
  • The android platform tools for easy installation of the .apk file

Installation

First make sure to have jobs: $ncpus commented out in ~/.cabal/config.

Then run the following commands:

git clone --recursive https://github.com/EDeijl/CPHConsoleApp.git
cd CPHConsoleApp
./setup.sh

The setup.sh script installs ghc-ios dependencies (when on OSX) and creates a docker container for Android builds.

Building

iOS

Run ./prepare-ios-all.sh to build the Haskell code of the project. This will put several library (.a) and header (.h) files in the Xcode project directory (./platforms/ios/).

Use Xcode to open the project and check if the lib directory in your project contains HaskellActivity_stub.h and CPConsoleAppStaticLib.a. It is no problem these files also exist in the root of the project. If the library and header files cannot be found in your project, then click File -> Add files to project, select the lib folder and make sure that Add to targets is enabled. In case you do not see Add to target, click options at the bottom of the file open dialog.

In the HaskelliOS.xcconfig (which can be found in the Xcode project folder), the HEADER_SEARCH_PATHS variable should point to the include folder of your GHC installation, otherwise Xcode is not able to compile the project. The include folder is found in the location where your GHC is installed. It's path usually end with something like lib/ghc-7.10.2/include/ and contains several header files (we include HsFFI.h).

Xcode should now be able to build and run the project. If all went well, the application is automatically run. In the terminal output it will read "Hello World!".

Android

For the Android build we make use of a Docker image that has all the dependencies for this project installed. The setup.sh script has already prepared this docker image for us.

With the following commands we start a container, build the .apk file, copy it to our host machine and remove the container so it will not conflict with the next build.

docker run --name apkbuilder -a stdout -a stdin android_build_env \
    /bin/bash -cl "cd CPConsoleApp; ./build-android.sh"
docker cp \
    apkbuilder:/home/androidbuilder/build/CPConsoleApp/platforms/android/bin/HaskellActivity-debug.apk .
docker rm apkbuilder

Now that we have the .apk on our local machine, we can install it on a device with adb install -r HaskellActivity-debug.apk. If a device is attached it will install the package there, otherwise it will try to run an emulator.

Desktop

For desktop builds just run cabal configure -fdesktop and cabal build to generate an executable.

Credits

This is mainly a merger to two existing examples of compiling Haskell to mobile devices:

CJ van den Berg's ghc-android, android-haskell-activity and foreign-jni.

Sean Seefried's docker image for android builds and iOS build scripts in his Epidemic game.

Without their prior work this would have not been possible.

TODO

  • Extend this example with SDL and networking

cphconsoleapp's People

Contributors

edeijl avatar

Watchers

James Cloos avatar Eric Moody avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.