Saturday, July 7, 2018

How to make a bootable macOS installer on USB in macOS Mojave

To create a bootable installer of the macOS on USB drive is useful to repair filesystem when another mac is not bootable.

Step 1: A 12GB Flash Drive (at least!) and formated with Mac OS Extended (Journaled) and Choose GUID Partition Map as the Scheme.
The name of the USB Flash Drive will be named as Untitled as default

Step 2: Go to Mac App Store and from the past purchase history, see if there is any Sierra Developer Beta in purchase history and then download it. For unknown reasons, the previous versions of macOS cannot be downloaded in the Mac App Store of Mojave.
There is one more rule : "A Mac can boot NO version OLDER than the version it shipped with". So choose the newer version.


Install_macOS_Sierra_Developer_Beta.rar (4.40GB)
https://mega.nz/#!yk4lSSRQ!WoOSpLf5BSlRR4if3RrbHVHQptG0Tfmw0Bnx4BCrHlA
Install_macOS_High_Sierra.rar (4.86GB)
https://mega.nz/#!WopVXYqQ!LlfKompmLDag20CE6UrsYQmL6e9mKoEgW08bLAvcnbs
Install_macOS_Mojave_Beta.rar (5.22GB)
https://mega.nz/#!qtwxkS7T!7_lG6VhwQLL1Zyc_-s_T5jjVu06vnnvHJTsSTa7fNiI

Step 3: Go to Terminal and type command
shellscript    Select all
# for Sierra Developer beta the command is sudo /Applications/Install\ macOS\ Sierra\ Developer\ Beta.app/Contents/Resources/createinstallmedia --volume /Volumes/Untitled --applicationpath /Applications/Install\ macOS\ Sierra\ Developer\ Beta.app # for High Sierra the command is sudo /Applications/Install\ macOS\ High\ Sierra.app/Contents/Resources/createinstallmedia --volume /Volumes/Untitled --applicationpath /Applications/Install\ macOS\ High\ Sierra.app # for Mojave beta the command is sudo /Applications/Install\ macOS\ Mojave\ Beta.app/Contents/Resources/createinstallmedia --volume /Volumes/Untitled --nointeraction --downloadassets


Step 4: Use the bootable macOS installer USB in a mac and press Option key when boot and use terminal to repair disk or filesystem. The reason to use High Sierra or above is that it can mount the new Apple File System (APFS).

Friday, June 29, 2018

How to install CocoaPods for macOS 10.14 beta


shell script    Select all
# update gem sudo gem update --system # Operation not permitted error # yes do it twice sudo gem update --system # install cocoapods sudo gem install -n /usr/local/bin cocoapods # install dependencies for project cd ~/MyProject pod install # if re-clone CocoaPods repo spec cd ~/.cocoapods/repos/ rm -fr master/ git clone --depth 1 https://github.com/CocoaPods/Specs.git master


Sunday, June 17, 2018

How to install turicreate on macOS 10.14 beta

Install turicreate on macOS 10.14 beta 1
shell script    Select all
# upgrade pip # curl https://bootstrap.pypa.io/get-pip.py | sudo python curl https://bootstrap.pypa.io/get-pip.py | python # install packages sudo pip install requests==2.18.4 turicreate==5.0b1


(1) Test turicreate example - Image Classifier
shell script    Select all
mkdir -p $HOME/MLClassifier cd $HOME/MLClassifier # download dataset and cleanup curl -L -o dataset.zip https://drive.google.com/uc?id=1ZLigrn7YcETalcj2qK6UqXceDdOV3244&export=download unzip dataset.zip rm -fr __MACOSX; rm dataset/.DS_Store dataset/*/.DS_Store # create python script cat > classifier.py << 'EOF' import turicreate as turi # load images from dataset folder url = "dataset/" data = turi.image_analysis.load_images(url) # define image categories data["foodType"] = data["path"].apply(lambda path: "Rice" if "rice" in path else "Soup") # create sframe data.save("rice_or_soup.sframe") # preview dataset data.explore() # load sframe dataBuffer = turi.SFrame("rice_or_soup.sframe") # create training data using 90% of dataset trainingBuffers, testingBuffers = dataBuffer.random_split(0.9) # create model model = turi.image_classifier.create(trainingBuffers, target="foodType", model="squeezenet_v1.1", max_iterations=100) # Alternate model use ResNet-50 # model = turi.image_classifier.create(trainingBuffers, target="foodType", model="resnet-50") # evaluate model evaluations = model.evaluate(testingBuffers) print evaluations["accuracy"] # save model model.save("rice_or_soup.model") model.export_coreml("RiceSoupClassifier.mlmodel") EOF #run script python classifier.py


(2) Test turicreate example - Logistic Regression
shell script    Select all
mkdir -p $HOME/LGClassifier cd $HOME/LGClassifier # create python script cat > classifier.py << 'EOF' import turicreate as turi data = turi.SFrame('http://static.turi.com/datasets/regression/yelp-data.csv') data['is_good'] = data['stars'] >= 3 # create sframe data.save("yelp.sframe") # preview dataset #data.show() # load sframe dataBuffer = turi.SFrame("yelp.sframe") # create training data using 80% of dataset train_data, test_data = dataBuffer.random_split(0.8) # create model model=turi.logistic_classifier.create(train_data, target='is_good', features = ['user_avg_stars', 'business_avg_stars', 'user_review_count', 'business_review_count', 'city', 'categories_dict'], max_iterations=200) print model # save predictions predictions = model.classify(test_data) print predictions # evaluate model evaluations = model.evaluate(test_data) print "Accuracy : %s" % evaluations["accuracy"] print "Confusion Matrix : \n%s" % evaluations["confusion_matrix"] EOF #run script python classifier.py


(3) Some data manipulation tips when preparing training data
shell script    Select all
# remove the quotes (replace the number with the quotes with the number without them) in csv file, typically "save as CSV" from excel file. # for example, "222,267.87","455,365.44",... convert to 222267.87,455365.44,... #In shell script cat exceldata.csv | perl -p -e 's/,(?=[\d,.]*\d")//g and s/"(\d[\d,.]*)"/\1/g' > dataset.csv # use map, lambda and zip functions when convert and compute numeric data from 2 data columns #In python script import math data['rate'] = map(lambda (x,y): 0 if x is None or y is None else (0 if math.isnan(x) or math.isnan(y) or math.isinf(y) or x==0 else (999999 if math.isinf(x) or y==0 else 999999 if x/y > 999999 else x/y)) , zip(data['OS'], data['Total Amount'])) # replace training data when values are inf(infinity) or nan(Not A Number) in 'amount' column #In python script import math train_data['amount'] = train_data['amount'].apply(lambda x: 0 if math.isnan(x) else x) train_data['amount'] = train_data['amount'].apply(lambda x: 999 if math.isinf(x) else x) # or use nested if else #In python script import math train_data['amount'] = train_data['amount'].apply(lambda x: 0 if math.isnan(x) else (999 if math.isinf(x) else x )) print train_data['amount'].summary() # remove rows in training data with inf(infinity) or nan(Not A Number) values in 'amount' column #In python script import math train_data = train_data[train_data['amount'].apply(lambda x: 0 if math.isinf(x) or math.isnan(x) else 1)] # SFrame methods but beware, some of the methods are not working https://apple.github.io/turicreate/docs/api/generated/turicreate.SFrame.html # Other SFrame data manipulation examples https://github.com/apple/turicreate/blob/master/userguide/sframe/data-manipulation.md


(4) Some data examination tips
shell script    Select all
# summary print train_data['amount'].summary() # crosstab import pandas as pd pd.crosstab(data["Rating"], data["is_bad"], margins=True) # custom frequency count for 'amount' column import pandas as pd pd.crosstab(train_data['amount'].apply(lambda x: " 0-10" if x <=10 else ("10-20" if x <=20 else ("20-30" if x <=30 else ("30-40" if x <=30 else ("40-50" if x <=50 else ">50"))))), "Count")


Saturday, June 9, 2018

Playground examples for XCode 10 Beta 1

Playground Support for iOS12 and Swift 4.2
iOS.playground    Select all
import UIKit import PlaygroundSupport //: **Markup** //: ### Define UIView class MyView : UIView { @objc public func changeTitle(_ sender:UIButton!) { sender.setTitle("Welcome to WWDC2018", for: []) } } let myView = MyView(frame: CGRect(x:0, y:0, width:500, height:500)) var button = UIButton(type: .system) button.frame = CGRect(x:100, y:100, width:300, height:200) button.setTitle("Hi Press me!", for: []) button.tintColor = .blue button.setTitleColor(.orange, for: []) button.addTarget(myView, action: #selector(MyView.changeTitle(_:)), for: .touchUpInside) myView.addSubview(button) PlaygroundPage.current.liveView = myView




Playground Icon Drawings in iOS and Swift 4.2
macOS.playground    Select all
import UIKit //: Define IconView class IconView: UIView { override func draw(_ rect: CGRect) { drawRawBackgroundWithBaseColor(strokeColor: UIColor.orange, backgroundRectangle: self.bounds) let textAttributes: [NSAttributedString.Key : Any] = [ NSAttributedString.Key.foregroundColor: UIColor.red, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 32.0)] let FString: String = "Hello World" let distanceX: CGFloat = -12.0 let distanceY: CGFloat = 0.0 let centerX = self.bounds.midX let centerY = self.bounds.midY FString.draw(at: CGPoint(x:centerX+distanceX, y:centerY+distanceY), withAttributes: textAttributes) } } func drawRawBackgroundWithBaseColor(strokeColor: UIColor, backgroundRectangle:CGRect) { let lineWidth = backgroundRectangle.width/36.0 let cornerRadius = backgroundRectangle.width/16.0 let tileRectangle = backgroundRectangle.insetBy(dx: lineWidth/2.0, dy: lineWidth/2.0) // Stroke Drawing let strokePath = UIBezierPath(roundedRect:tileRectangle, cornerRadius:cornerRadius) strokeColor.setStroke() strokePath.lineWidth = lineWidth strokePath.stroke() // Draw an ellipse let ovalPath = UIBezierPath(ovalIn: backgroundRectangle.insetBy(dx: lineWidth*1.5, dy: lineWidth*1.5)) UIColor.blue.setStroke() ovalPath.lineWidth = lineWidth ovalPath.stroke() let context:CGContext = UIGraphicsGetCurrentContext()! context.setFillColor(UIColor.green.cgColor) context.addRect(CGRect(x: 100.0, y: 100.0, width: 60.0, height: 60.0)) context.fillPath() } //: Instantiate the UIView let rect = CGRect(x: 0.0, y: 0.0, width: 420.0, height: 320.0) let icon = IconView(frame: rect) icon.backgroundColor = UIColor.clear




CreateML for macOS 10.14 Beta 1 (requires macOS 10.14 Mojave)
macOS.playground    Select all
import Cocoa import CreateML //: Specify Data /* Input as CSV mycsv.csv: beds,baths,squareFt, price 2,2,2000,400000 4,3,2500,500000 3,2,1800,450000 3,2,1500,300000 let houseData = try MLDataTable(contentsOf: URL(fileURLWithPath: "mycsv.csv")) */ //: Input as dictionary let mydata : [String: MLDataValueConvertible] = [ "beds": [2,4,3,3], "baths": [2,3,2,2], "squareFt": [2000,2500,1800,1500], "price": [400000,500000,450000,300000] ] let houseData = try MLDataTable(dictionary:mydata) let (trainingData, testData) = houseData.randomSplit(by: 0.8, seed: 0) //: Create Model let pricer = try MLRegressor(trainingData: houseData, targetColumn: "price") //: Evaluate Model let evalator = pricer.evaluation(on: testData) print(pricer) //: Save Model try pricer.write(to:URL(fileURLWithPath: "HousePricer.mlmodel"))




Tuesday, July 4, 2017

How to fetch WWDC 2017/2018 Video Subtitle to SRT format

Create and Run this script wwdc_fetch_srt.sh to fetch WWDC2018 subtitle
Reference : https://github.com/wsvn53/wwdc2016-subtitles

wwdc_fetch_srt.sh    Select all
#!/bin/sh # @Author: Ethan # @Date: 2016-06-22 14:10:53 # @Last Modified by: javacom # @Last Modified time: 2018-06-26 WWDC_YEAR=2018; # change to 2017 and also works for WWDC2017 WWDC_SESSION_PREFIX=https://developer.apple.com/videos/play/wwdc$WWDC_YEAR; WWDC_LOCAL_DIR=$(basename $WWDC_SESSION_PREFIX); detect_video_m3u8 () { local session_url=$WWDC_SESSION_PREFIX/$SESSION_ID/; local session_html=$(curl -s $session_url); local video_url=$(echo "$session_html" | grep .m3u8 | grep $SESSION_ID | head -n1 | sed "s#.*\"\(https://.*m3u8\)\".*#\1#"); echo "$session_html" | grep .mp4 | grep $SESSION_ID | sed "s#.*\"\(https://.*mp4\).*\".*#\1#" | while read mp4_url; do local mp4_filename=$(basename $mp4_url | cut -d. -f1); local srt_filename=$mp4_filename.srt; echo "> Subtitle local: $WWDC_LOCAL_DIR/$srt_filename" >&2; > $WWDC_LOCAL_DIR/$srt_filename; done echo "$video_url"; echo "> Video: $video_url" >&2; } detect_subtitle_m3u8 () { local video_url=$1; local subtitle_uri=$(curl -s $video_url | grep "LANGUAGE=\"eng\"" | sed "s#.*URI=\"\(.*\)\"#\1#"); local subtitle_url=$subtitle_uri; [[ "$subtitle_uri" != http* ]] && { subtitle_url=$(dirname $video_url)/$subtitle_uri; } echo "$subtitle_url"; echo "> Subtitle: $subtitle_url" >&2; } download_subtitle_contents () { local subtitle_url=$1; echo "> Downloading... " local subtitle_base_url=$(dirname $subtitle_url); curl -s $subtitle_url | grep "webvtt" | while read webvtt; do local subtitle_webvtt=$subtitle_base_url/$webvtt; #echo "- get $subtitle_webvtt"; local subtitle_content=$(curl -s $subtitle_webvtt); ls $WWDC_LOCAL_DIR/"$SESSION_ID"_* | while read srt_file; do echo "$subtitle_content" >> $srt_file; done done } main () { [ ! -d $WWDC_LOCAL_DIR ] && { mkdir $WWDC_LOCAL_DIR; } curl -s $WWDC_SESSION_PREFIX | grep /videos/play/wwdc$WWDC_YEAR | sed "s#.*/videos/play/wwdc$WWDC_YEAR/\([0-9]\{3\}\).*#\1#" | sort | uniq | while read SESSION_ID; do #echo "SESSION_ID is" $SESSION_ID local video_url=$(detect_video_m3u8 $SESSION_ID); local subtitle_url=$(detect_subtitle_m3u8 $video_url); download_subtitle_contents $subtitle_url; done } main;




Run this shell script to format as SRT subtitle

shellscript.sh    Select all
WWDC_YEAR=2018; # change to 2017 and also works for WWDC2017 cd wwdc$WWDC_YEAR mkdir -p sd mkdir -p hd for i in ???_sd_*.srt; do sed -e '/WEBVTT/d;/X-TIMESTAMP/d;' $i | awk '/^[0-9]{2}:[0-9]{2}:/ {seen[$0]++; skipduplicated=0} {if (seen[$0]>1) skipduplicated=1; if (!skipduplicated) print $0}' | awk -v RS="" '{gsub("\n", "-Z"); print}' | awk '$0 !~/^WEB/ {print $0}' | uniq | awk '{printf "\n%s-Z%s", NR,$0 }' | awk -v ORS="\n\n" '{gsub("-Z", "\n"); print}' | sed -e 's/.A:middle$//g;s/&gt;/>/g;s/&lt;/</g;1,2d;' > sd/$i; done for i in ???_hd_*.srt; do sed -e '/WEBVTT/d;/X-TIMESTAMP/d;' $i | awk '/^[0-9]{2}:[0-9]{2}:/ {seen[$0]++; skipduplicated=0} {if (seen[$0]>1) skipduplicated=1; if (!skipduplicated) print $0}' | awk -v RS="" '{gsub("\n", "-Z"); print}' | awk '$0 !~/^WEB/ {print $0}' | uniq | awk '{printf "\n%s-Z%s", NR,$0 }' | awk -v ORS="\n\n" '{gsub("-Z", "\n"); print}' | sed -e 's/.A:middle$//g;s/&gt;/>/g;s/&lt;/</g;1,2d;' > hd/$i; done




Run this script wwdc_fetch_mp4.sh to download all mp4 videos

wwdc_fetch_mp4.sh    Select all
#!/bin/sh # @Last Modified by: javacom # @Last Modified time: 2018-06-26 WWDC_YEAR=2018; # change to 2017 and also works for WWDC2017 WWDC_SESSION_PREFIX=https://developer.apple.com/videos/play/wwdc$WWDC_YEAR; WWDC_LOCAL_DIR=$(basename $WWDC_SESSION_PREFIX); download_mp4_video () { local session_url=$WWDC_SESSION_PREFIX/$SESSION_ID/; local session_html=$(curl -s $session_url); local video_url=$(echo "$session_html" | grep .m3u8 | grep $SESSION_ID | head -n1 | sed "s#.*\"\(https://.*m3u8\)\".*#\1#"); echo "$session_html" | grep .mp4 | grep $SESSION_ID | sed "s#.*\"\(https://.*mp4\).*\".*#\1#" | while read mp4_url; do local mp4_filename=$(basename $mp4_url); if [ -e $WWDC_LOCAL_DIR/$mp4_filename ] then echo "> MP4 already existed : $WWDC_LOCAL_DIR/$mp4_filename" >&2; else echo "> MP4 Downloading... : $mp4_url" >&2; curl -o $WWDC_LOCAL_DIR/$mp4_filename $mp4_url fi done } main () { [ ! -d $WWDC_LOCAL_DIR ] && { mkdir $WWDC_LOCAL_DIR; } curl -s $WWDC_SESSION_PREFIX | grep /videos/play/wwdc$WWDC_YEAR | sed "s#.*/videos/play/wwdc$WWDC_YEAR/\([0-9]\{3\}\).*#\1#" | sort | uniq | while read SESSION_ID; do download_mp4_video $SESSION_ID; done } main;


One line version wwdc2017_fetch_mp4.sh to download all mp4 videos

wwdc2017_fetch_mp4.sh    Select all
# one liner for hd videos download # change to wwdc2017 and also works for WWDC2017 WWDCYEAR="wwdc2018"; for i in `curl -s https://developer.apple.com/videos/$WWDCYEAR/ | grep -o '<a href="/videos/play/'"$WWDCYEAR"'/[0-9]*' | cut -d '"' -f2 | sort | uniq`; do video_url=$(curl -s https://developer.apple.com${i} | grep -o 'http.*_hd_.*.mp4'); if [ ! -z "$video_url" ]; then mp4_filename=$(basename $video_url); if [ -e $mp4_filename ]; then echo "skipping $mp4_filename"; else echo "Downloading ... $mp4_filename";curl -O $video_url; fi; fi; done # one liner for sd videos download WWDCYEAR="wwdc2018"; for i in `curl -s https://developer.apple.com/videos/$WWDCYEAR/ | grep -o '<a href="/videos/play/'"$WWDCYEAR"'/[0-9]*' | cut -d '"' -f2 | sort | uniq`; do video_url=$(curl -s https://developer.apple.com${i} | grep -o 'http.*_sd_.*.mp4'); if [ ! -z "$video_url" ]; then mp4_filename=$(basename $video_url); if [ -e $mp4_filename ]; then echo "skipping $mp4_filename"; else echo "Downloading ... $mp4_filename";curl -O $video_url; fi; fi; done




Wednesday, June 7, 2017

How to train dataset in python and convert to CoreML model for iOS11

Reference http://machinelearningmastery.com/machine-learning-in-python-step-by-step/

Environment : macOS 10.12.4
matplotlib==2.0.0
numpy==1.12.1
pandas==0.19.2
scikit-learn==0.18.1
scipy==0.19.0
six==1.10.0
sklearn==0.18.1
coremltools==0.3.0
protobuf==3.3.0

Upgrade the pip and install the following python packages
shellscript.sh    Select all
pip install --upgrade pip sudo -H pip install numpy scipy matplotlib pandas sklearn coremltools protobuf



Convert to Core ML Run the following python code to show machine learning in python step by step and finally generate iris_lr.mlmodel
iris_learn.py    Select all
#!/usr/bin/env python # Check the versions of libraries # Python version import sys print('Python: {}'.format(sys.version)) # scipy import scipy print('scipy: {}'.format(scipy.__version__)) # numpy import numpy print('numpy: {}'.format(numpy.__version__)) # matplotlib import matplotlib print('matplotlib: {}'.format(matplotlib.__version__)) # pandas import pandas print('pandas: {}'.format(pandas.__version__)) # scikit-learn import sklearn print('sklearn: {}'.format(sklearn.__version__)) # Load libraries import pandas from pandas.tools.plotting import scatter_matrix import matplotlib.pyplot as plt from sklearn import model_selection from sklearn.metrics import classification_report from sklearn.metrics import confusion_matrix from sklearn.metrics import accuracy_score from sklearn.linear_model import LogisticRegression from sklearn.tree import DecisionTreeClassifier from sklearn.neighbors import KNeighborsClassifier from sklearn.discriminant_analysis import LinearDiscriminantAnalysis from sklearn.naive_bayes import GaussianNB from sklearn.svm import SVC # Load dataset url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data" names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class'] dataset = pandas.read_csv(url, names=names) # shape print(dataset.shape) # head print(dataset.head(20)) # descriptions print(dataset.describe()) # class distribution print(dataset.groupby('class').size()) # box and whisker plots dataset.plot(kind='box', subplots=True, layout=(2,2), sharex=False, sharey=False) plt.suptitle("Box and Whisker Plots for inputs") plt.show() # histograms dataset.hist() plt.suptitle('Histograms for inputs') plt.show() # scatter plot matrix scatter_matrix(dataset) plt.suptitle('Scatter Plot Matrix for inputs') plt.show() # Split-out validation dataset array = dataset.values X = array[:,0:4] Y = array[:,4] validation_size = 0.20 seed = 7 X_train, X_validation, Y_train, Y_validation = model_selection.train_test_split(X, Y, test_size=validation_size, random_state=seed) # Test options and evaluation metric seed = 7 scoring = 'accuracy' # Spot Check Algorithms models = [] models.append(('LR', LogisticRegression())) models.append(('LDA', LinearDiscriminantAnalysis())) models.append(('KNN', KNeighborsClassifier())) models.append(('CART', DecisionTreeClassifier())) models.append(('NB', GaussianNB())) models.append(('SVM', SVC())) # evaluate each model in turn results = [] names = [] for name, model in models: kfold = model_selection.KFold(n_splits=10, random_state=seed) cv_results = model_selection.cross_val_score(model, X_train, Y_train, cv=kfold, scoring=scoring) results.append(cv_results) names.append(name) msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std()) print(msg) # Compare Algorithms fig = plt.figure() fig.suptitle('Algorithm Comparison') ax = fig.add_subplot(111) plt.boxplot(results) ax.set_xticklabels(names) plt.show() # Make predictions on validation dataset knn = KNeighborsClassifier() knn.fit(X_train, Y_train) predictions = knn.predict(X_validation) print(accuracy_score(Y_validation, predictions)) print(confusion_matrix(Y_validation, predictions)) print(classification_report(Y_validation, predictions)) print("Make predictions on LogisticRegression Model") model = LogisticRegression() model.fit(X_train, Y_train) predictions = model.predict(X_validation) print(accuracy_score(Y_validation, predictions)) print(confusion_matrix(Y_validation, predictions)) print(classification_report(Y_validation, predictions)) # print prediction results on test data for i, prediction in enumerate(predictions): print 'Predicted: %s, Target: %s %s' % (prediction, Y_validation[i], '' if prediction==Y_validation[i] else '(WRONG!!!)') #convert and save scikit.learn model #support LogisticRegression of scikit.learn print("Convert LogisticRegression Model to coreml model") import coremltools coreml_model = coremltools.converters.sklearn.convert(model, ["sepal-length", "sepal-width", "petal-length", "petal-width"], "class") #set model metadata coreml_model.author = 'Author' coreml_model.license = 'BSD' coreml_model.short_description = 'LogisticRegression on Iris flower data set' #set features description manually coreml_model.input_description['sepal-length'] = 'Sepal Length in centimetres' coreml_model.input_description['sepal-width'] = 'Sepal Width in centimetres' coreml_model.input_description['petal-length'] = 'Petal Length in centimetres' coreml_model.input_description['petal-width'] = 'Petal Width in centimetres' #set the ouput description coreml_model.output_description['class'] = 'Distinguish the species' #save the model coreml_model.save('iris_lr.mlmodel') from coremltools.models import MLModel model = MLModel('iris_lr.mlmodel') #get the spec of the model print(model.get_spec())


Download Xcode 9 beta and the sample code from Apple

https://docs-assets.developer.apple.com/published/51ff0c1668/IntegratingaCoreMLModelintoYourApp.zip
Modify it and add the model to the xcode project


Try the new refactoring tool in Xcode 9. It is amazing.


Train data using Neural Network Model Keras
Reference : http://machinelearningmastery.com/5-step-life-cycle-neural-network-models-keras/

shellscript.sh    Select all
# download training data curl -O http://archive.ics.uci.edu/ml/machine-learning-databases/pima-indians-diabetes/pima-indians-diabetes.data # install and activate virtual environment and install necessary python packages # use deactivate to stop the python virtual env sudo -H pip install --upgrade virtualenv virtualenv --system-site-packages ~/tensorflow source ~/tensorflow/bin/activate # macOS, CPU only non-optimised, Python 2.7: # https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.1.0-py2-none-any.whl # macOS, GPU enabled, Python 2.7: # https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow_gpu-1.1.0-py2-none-any.whl # or find optimised wheel files from the community https://github.com/yaroslavvb/tensorflow-community-wheels/issues # this optimised one (SSE4.1,SSE4.2,AVX,AVX2,FMA) works for Python 2.7 macOS 10.12 Tensoflow 1.1.0 CPU https://github.com/fdalvi/tensorflow-builds # this one works for GeForce GT 650M GPU and CPU (SSE4.2, AVX) and CUDA 8.0, and cuDNN v5.1 https://github.com/bodak/tensorflow-wheels/releases/tag/v1.1.0_27 # instruction to build your own python package https://ctmakro.github.io/site/on_learning/tf1c.html # suppose, install the official non-optimised wheel file as below pip install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.1.0-py2-none-any.whl pip install coremltools protobuf pip install keras==1.2.2 h5py

Convert to Core ML Run the following python code in virtual environment (tensorflow) to generate pima_keras.mlmodel
keras_learn.py    Select all
#!/usr/bin/env python from keras.models import Sequential from keras.layers import Dense import numpy # fix random seed for reproducibility numpy.random.seed(7) # load pima indians dataset #dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",") dataset = numpy.loadtxt("pima-indians-diabetes.data", delimiter=",") # split into input (X) and output (Y) variables X = dataset[:,0:8] Y = dataset[:,8] # create model model = Sequential() model.add(Dense(12, input_dim=8, activation='relu')) model.add(Dense(1, activation='sigmoid')) # Compile model model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) # Fit the model #model.fit(X, Y, epochs=150, batch_size=10) model.fit(X, Y, 10, 150) # parameters change to keras 1.2.2 # evaluate the model scores = model.evaluate(X, Y) print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100)) #convert and save keras model model.save('pima.h5') print("Convert Model to coreml model") import coremltools coreml_model = coremltools.converters.keras.convert('pima.h5') #set model metadata coreml_model.author = 'Author' coreml_model.license = 'BSD' coreml_model.short_description = 'pima-indians-diabetes' #save the model coreml_model.save('pima_keras.mlmodel') from coremltools.models import MLModel mlmodel = MLModel('pima_keras.mlmodel') #get the spec of the model print(mlmodel.get_spec())


Note: coremltools require python 2.7 (not for 3.x) and supports keras==1.2.2 with Tenorflow (1.0.x, 1.1.x) only. Tenorflow_gpu requires Nvidia Cuda 8.0 and cuDNN v5.1 (which also requires macOS 10.11/10.12) but recent models of Mac are all bundled AMD GPUs. Unless you could get an old Mac Pro with upgraded Nvidia GPU with at least 4 GB of video RAM, it is better to stay with Mac CPU i7 or get a Linux machine for data training purpose only.

Hardware reference for Linux : https://www.oreilly.com/learning/build-a-super-fast-deep-learning-machine-for-under-1000

For Windows PC, tensorflow/tensorflow_gpu is only available for Python 3.5 and 64 bits only as below. As current coremltools keras convertors are not compatible with python 3.5, so direct conversion is not available in PC yet.
https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.1.0-cp35-cp35m-win_amd64.whl
https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-1.1.0-cp35-cp35m-win_amd64.whl



keras-inception-test Run the following python code in virtual environment (tensorflow) to test Keras Inceptionv3 model. This will download the trained Inception V3 weights from https://github.com/fchollet/deep-learning-models/releases/download/v0.2/inception_v3_weights_tf_dim_ordering_tf_kernels.h5
shellscript.sh    Select all
git clone git://github.com/vml-ffleschner/coremltools-keras-inception-test cd coremltools-keras-inception-test/ # based on the created virtualenv in ~/tensorflow as above source ~/tensorflow/bin/activate # additional installation of packages pip install olefile pillow #Add coreml_model.author = 'Author' coreml_model.license = 'BSD' coreml_model.short_description = 'Image InceptionV3 model' coreml_model.save('Inceptionv3.mlmodel') print("CoreML model file Created") #After #print("CoreML Converted") #in playground.py # note : coreml_model.predict requires macOS 10.13 High Sierra python playground.py


Install tensorflow 1.1.0 library for Java is here
shellscript.sh    Select all
curl -O https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-1.1.0.jar curl -O https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-cpu-darwin-x86_64-1.1.0.tar.gz
# install tar xzvf libtensorflow_jni-cpu-darwin-x86_64-1.1.0.tar.gz -C ./jni # compile and run HelloTF javac -cp libtensorflow-1.1.0.jar HelloTF.java java -cp libtensorflow-1.1.0.jar:. -Djava.library.path=./jni HelloTF


Monday, November 28, 2016

How to build c++ libraries (boost & QuantLib) for iOS XCode 8/9

It is possible to skip the armv7, armv7s and i386 architectures if these binaries are not required. Just modify the relevant part of the scripts below

(1) Download boost 1.62.0 and extract source
shell script    Select all
# download boost source cd $HOME/Downloads curl -O https://nchc.dl.sourceforge.net/project/boost/boost/1.62.0/boost_1_62_0.tar.bz2 tar xjvf boost_1_62_0.tar.bz2 cd boost_1_62_0 # create the following user-config.jam at ~/Downloads/boost_1_62_0/tools/build/src/ cat > tools/build/src/user-config.jam << 'EOF' # ------------------ # darwin iphone configuration. # ------------------ local XCODE_ROOT = /Applications/Xcode.app/Contents/Developer ; # xcrun --sdk iphoneos --show-sdk-path local IPHONESDK = iPhoneOS.sdk ; # xcrun --sdk iphonesimulator --show-sdk-path local IPHONESIMULATORSDK = iPhoneSimulator.sdk ; using darwin : armv7 : $(XCODE_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ : <striper>$(XCODE_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/strip <root>$(XCODE_ROOT)/Platforms/iPhoneOS.platform/Developer/SDKs/$(IPHONESDK) <compileflags>"-isysroot $(XCODE_ROOT)/Platforms/iPhoneOS.platform/Developer/SDKs/$(IPHONESDK)" <compileflags>"-arch armv7" <compileflags>-fexceptions <compileflags>-frtti <compileflags>-fpic <compileflags>-DBOOST_SP_USE_SPINLOCK <compileflags>-std=c++11 <compileflags>-stdlib=libc++ <compileflags>-miphoneos-version-min=7.0 <compileflags>-fvisibility=hidden <compileflags>-fvisibility-inlines-hidden <compileflags>-O2 <compileflags>-g <compileflags>-Os <compileflags>-Wno-long-long <compileflags>-Wno-missing-field-initializers <compileflags>-Wno-unused-variable <linkflags>-stdlib=libc++ <architecture>arm <address-model>32 <target-os>iphone ; using darwin : armv7s : $(XCODE_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ : <striper>$(XCODE_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/strip <root>$(XCODE_ROOT)/Platforms/iPhoneOS.platform/Developer/SDKs/$(IPHONESDK) <compileflags>"-isysroot $(XCODE_ROOT)/Platforms/iPhoneOS.platform/Developer/SDKs/$(IPHONESDK)" <compileflags>"-arch armv7s" <compileflags>-fexceptions <compileflags>-frtti <compileflags>-fpic <compileflags>-DBOOST_SP_USE_SPINLOCK <compileflags>-std=c++11 <compileflags>-stdlib=libc++ <compileflags>-miphoneos-version-min=7.0 <compileflags>-fvisibility=hidden <compileflags>-fvisibility-inlines-hidden <compileflags>-O2 <compileflags>-g <compileflags>-Os <compileflags>-Wno-long-long <compileflags>-Wno-missing-field-initializers <compileflags>-Wno-unused-variable <linkflags>-stdlib=libc++ <architecture>arm <address-model>32 <target-os>iphone ; using darwin : arm64 : $(XCODE_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ : <striper>$(XCODE_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/strip <root>$(XCODE_ROOT)/Platforms/iPhoneOS.platform/Developer/SDKs/$(IPHONESDK) <compileflags>"-isysroot $(XCODE_ROOT)/Platforms/iPhoneOS.platform/Developer/SDKs/$(IPHONESDK)" <compileflags>"-arch arm64" <compileflags>-fexceptions <compileflags>-frtti <compileflags>-fpic <compileflags>-DBOOST_SP_USE_SPINLOCK <compileflags>-std=c++11 <compileflags>-stdlib=libc++ <compileflags>-miphoneos-version-min=7.0 <compileflags>-fvisibility=hidden <compileflags>-fvisibility-inlines-hidden <compileflags>-O2 <compileflags>-g <compileflags>-Os <compileflags>-Wno-long-long <compileflags>-Wno-missing-field-initializers <compileflags>-Wno-unused-variable <linkflags>-stdlib=libc++ <architecture>arm <address-model>64 <target-os>iphone ; using darwin : i386 : $(XCODE_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ : <striper>$(XCODE_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/strip <root>$(XCODE_ROOT)/Platforms/iPhoneSimulator.platform/Developer/SDKs/$(IPHONESIMULATORSDK) <compileflags>"-arch i386" <compileflags>"-isysroot $(XCODE_ROOT)/Platforms/iPhoneSimulator.platform/Developer/SDKs/$(IPHONESIMULATORSDK)" <compileflags>-fexceptions <compileflags>-frtti <compileflags>-fpic <compileflags>-DBOOST_SP_USE_SPINLOCK <compileflags>-std=c++11 <compileflags>-stdlib=libc++ <compileflags>-miphoneos-version-min=7.0 <compileflags>-fvisibility=hidden <compileflags>-fvisibility-inlines-hidden <compileflags>-O2 <compileflags>-g <compileflags>-Os <compileflags>-Wno-long-long <compileflags>-Wno-missing-field-initializers <compileflags>-Wno-unused-variable <linkflags>-stdlib=libc++ <architecture>x86 <address-model>32 <target-os>iphone ; using darwin : x86_64 : $(XCODE_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ : <striper>$(XCODE_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/strip <root>$(XCODE_ROOT)/Platforms/iPhoneSimulator.platform/Developer/SDKs/$(IPHONESIMULATORSDK) <compileflags>"-arch x86_64" <compileflags>"-isysroot $(XCODE_ROOT)/Platforms/iPhoneSimulator.platform/Developer/SDKs/$(IPHONESIMULATORSDK)" <compileflags>-fexceptions <compileflags>-frtti <compileflags>-fpic <compileflags>-DBOOST_SP_USE_SPINLOCK <compileflags>-std=c++11 <compileflags>-stdlib=libc++ <compileflags>-miphoneos-version-min=7.0 <compileflags>-fvisibility=hidden <compileflags>-fvisibility-inlines-hidden <compileflags>-O2 <compileflags>-g <compileflags>-Os <compileflags>-Wno-long-long <compileflags>-Wno-missing-field-initializers <compileflags>-Wno-unused-variable <linkflags>-stdlib=libc++ <architecture>x86 <address-model>64 <target-os>iphone ; EOF


(2) patch boost_1_62_0/tools/build/src/tools/darwin.jam
~/Downloads/boost_1_62_0/tools/build/src/tools/darwin.jam    Select all
#change from case arm : { if $(instruction-set) { options = -arch$(_)$(instruction-set) ; } else { options = -arch arm ; } } #to case arm : { if $(instruction-set) { options = -arch$(_)$(instruction-set) ; } else if $(address-model) = 64 { options = -arch arm64 ; } else { options = -arch arm ; } }


(3) build boost libraries for iOS
shell script    Select all
# bootstrap cd $HOME/Downloads/boost_1_62_0 ./bootstrap.sh --with-libraries=atomic,chrono,date_time,exception,filesystem,graph,iostreams,math,program_options,random,regex,serialization,signals,system,test,thread,wave # clean and build for iOS, j option is num of cores x 1.5 rm -fr ios-build ./b2 -j6 --build-dir=ios-build --stagedir=ios-build/armv7 toolset=darwin-armv7 architecture=arm instruction-set=armv7 address-model=32 target-os=iphone threading=multi link=static stage ./b2 -j6 --build-dir=ios-build --stagedir=ios-build/armv7s toolset=darwin-armv7s architecture=arm instruction-set=armv7s address-model=32 target-os=iphone threading=multi link=static stage ./b2 -j6 --build-dir=ios-build --stagedir=ios-build/arm64 toolset=darwin-arm64 architecture=arm address-model=64 target-os=iphone threading=multi link=static stage ./b2 -j6 --build-dir=ios-build --stagedir=ios-build/i386 toolset=darwin-i386 architecture=x86 address-model=32 target-os=iphone threading=multi link=static stage ./b2 -j6 --build-dir=ios-build --stagedir=ios-build/x86_64 toolset=darwin-x86_64 architecture=x86 address-model=64 target-os=iphone threading=multi link=static stage # create libboost.a archive for each architecture cd $HOME/Downloads/boost_1_62_0 cd ios-build xcrun --sdk iphoneos ar crus armv7/libboost.a boost/bin.v2/libs/*/build/darwin-armv7/release/instruction-set-armv7/link-static/target-os-iphone/threading-multi/*/*.o xcrun --sdk iphoneos ar crus armv7s/libboost.a boost/bin.v2/libs/*/build/darwin-armv7s/release/instruction-set-armv7s/link-static/target-os-iphone/threading-multi/*/*.o xcrun --sdk iphoneos ar crus arm64/libboost.a boost/bin.v2/libs/*/build/darwin-arm64/release/link-static/target-os-iphone/threading-multi/*/*.o xcrun --sdk iphoneos ar crus i386/libboost.a boost/bin.v2/libs/*/build/darwin-i386/release/link-static/target-os-iphone/threading-multi/*/*.o xcrun --sdk iphoneos ar crus x86_64/libboost.a boost/bin.v2/libs/*/build/darwin-x86_64/release/link-static/target-os-iphone/threading-multi/*/*.o # create FAT libboost.a archive mkdir -p lib /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo \ -arch armv7 "armv7/libboost.a" \ -arch armv7s "armv7s/libboost.a" \ -arch arm64 "arm64/libboost.a" \ -arch i386 "i386/libboost.a" \ -arch x86_64 "x86_64/libboost.a" \ -output "lib/libboost.a" \ -create # create link for include folder mkdir include cd include ln -s ../../boost .


(4) Create boost Framework for iOS
shell script    Select all
cd $HOME/Downloads/boost_1_62_0 cd ios-build #Create the following shell script for build-boostframework.sh cat > build-boostframework.sh << 'boostFrameworkEOF' #!/bin/bash # build-boostframework.sh VERSION_TYPE=Alpha FRAMEWORK_NAME=boost FRAMEWORK_VERSION=A FRAMEWORKDIR=. FRAMEWORK_CURRENT_VERSION=1.62.0 FRAMEWORK_COMPATIBILITY_VERSION=1.62.0 FRAMEWORK_BUNDLE=${FRAMEWORKDIR}/${FRAMEWORK_NAME}.framework rm -rf ${FRAMEWORK_BUNDLE} mkdir -p ${FRAMEWORK_BUNDLE} mkdir -p ${FRAMEWORK_BUNDLE}/Versions mkdir -p ${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION} mkdir -p ${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION}/Resources mkdir -p ${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION}/Headers mkdir -p ${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION}/Documentation ln -s $FRAMEWORK_VERSION ${FRAMEWORK_BUNDLE}/Versions/Current ln -s Versions/Current/Headers ${FRAMEWORK_BUNDLE}/Headers ln -s Versions/Current/Resources ${FRAMEWORK_BUNDLE}/Resources ln -s Versions/Current/Documentation ${FRAMEWORK_BUNDLE}/Documentation ln -s Versions/Current/$FRAMEWORK_NAME ${FRAMEWORK_BUNDLE}/$FRAMEWORK_NAME FRAMEWORK_INSTALL_NAME=${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION}/${FRAMEWORK_NAME} echo "Framework: Lipoing library into ${FRAMEWORK_INSTALL_NAME}" /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo \ -arch armv7 "armv7/libboost.a" \ -arch armv7s "armv7s/libboost.a" \ -arch arm64 "arm64/libboost.a" \ -arch i386 "i386/libboost.a" \ -arch x86_64 "x86_64/libboost.a" \ -output "${FRAMEWORK_INSTALL_NAME}" \ -create echo "Framework: Copying includes..." cp -r include/${FRAMEWORK_NAME}/* ${FRAMEWORK_BUNDLE}/Headers/ cat > ${FRAMEWORK_BUNDLE}/Resources/Info.plist <<InfoplistEOF <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CFBundleDevelopmentRegion</key> <string>English</string> <key>CFBundleExecutable</key> <string>${FRAMEWORK_NAME}</string> <key>CFBundleIdentifier</key> <string>${FRAMEWORK_NAME}.org</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundlePackageType</key> <string>FMWK</string> <key>CFBundleSignature</key> <string>????</string> <key>CFBundleVersion</key> <string>${FRAMEWORK_CURRENT_VERSION}</string> </dict> </plist> InfoplistEOF echo "Done: ${FRAMEWORK_BUNDLE}" boostFrameworkEOF # build framework sh build-boostframework.sh


(5) Download and build QuantLib 1.12 for iOS
shell script    Select all
# download quantlib source cd $HOME/Downloads # latest is https://jaist.dl.sourceforge.net/project/quantlib/QuantLib/1.12/QuantLib-1.12.tar.gz (February 1st, 2018) export QL_VERSION=1.12 curl -O https://jaist.dl.sourceforge.net/project/quantlib/QuantLib/${QL_VERSION}/QuantLib-${QL_VERSION}.tar.gz # curl -O https://jaist.dl.sourceforge.net/project/quantlib/QuantLib/1.9/QuantLib-1.9.tar.gz tar xzvf QuantLib-${QL_VERSION}.tar.gz cd QuantLib-${QL_VERSION} #Create the following shell script for armv7 cat > ~/Downloads/QuantLib-${QL_VERSION}/build-armv7.sh << 'armv7EOF' #!/bin/bash # build-armv7.sh BUILD_DIR=ios-build mkdir -p ${BUILD_DIR} INSTALL_DIR="`pwd`/${BUILD_DIR}" YOUR_PATH_TO_INSTALL_DIR="`cd "${INSTALL_DIR}";pwd`" XCODE_ROOT=/Applications/Xcode.app/Contents/Developer YOUR_TOOLCHAIN=${XCODE_ROOT}/Toolchains/XcodeDefault.xctoolchain/usr/bin # xcrun --sdk iphoneos --show-sdk-path # sysroot=${XCODE_ROOT}/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk sysroot="`xcrun --sdk iphoneos --show-sdk-path`" export CC=${YOUR_TOOLCHAIN}/clang export CXX=${YOUR_TOOLCHAIN}/clang++ export CPP=${YOUR_TOOLCHAIN}/cpp export AR=${YOUR_TOOLCHAIN}/ar export RANLIB=${YOUR_TOOLCHAIN}/ranlib export LD=${YOUR_TOOLCHAIN}/ld export CFLAGS="-arch armv7 -std=c99 -miphoneos-version-min=7.0 -isysroot ${sysroot}" export CXXFLAGS="-arch armv7 -fexceptions -frtti -fpic -std=c++11 -stdlib=libc++ -miphoneos-version-min=7.0 -O2 -g -fvisibility=hidden -fvisibility-inlines-hidden -Os -isysroot ${sysroot}" export LDFLAGS="-stdlib=libc++" boost_include="`cd "../boost_1_62_0/${BUILD_DIR}/include";pwd`" boost_lib="`cd "../boost_1_62_0/${BUILD_DIR}/armv7/lib";pwd`" make distclean ./configure --prefix=/armv7 --host=arm-apple-darwin10 --with-sysroot=${sysroot} --with-boost-include=${boost_include} --with-boost-lib=${boost_lib} --enable-static --enable-shared=no --disable-examples make -j6 make install DESTDIR=${YOUR_PATH_TO_INSTALL_DIR} armv7EOF #Create the following shell script for armv7s cat > ~/Downloads/QuantLib-${QL_VERSION}/build-armv7s.sh << 'armv7sEOF' #!/bin/bash # build-armv7s.sh BUILD_DIR=ios-build mkdir -p ${BUILD_DIR} INSTALL_DIR="`pwd`/${BUILD_DIR}" YOUR_PATH_TO_INSTALL_DIR="`cd "${INSTALL_DIR}";pwd`" XCODE_ROOT=/Applications/Xcode.app/Contents/Developer YOUR_TOOLCHAIN=${XCODE_ROOT}/Toolchains/XcodeDefault.xctoolchain/usr/bin # xcrun --sdk iphoneos --show-sdk-path # sysroot=${XCODE_ROOT}/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk sysroot="`xcrun --sdk iphoneos --show-sdk-path`" export CC=${YOUR_TOOLCHAIN}/clang export CXX=${YOUR_TOOLCHAIN}/clang++ export CPP=${YOUR_TOOLCHAIN}/cpp export AR=${YOUR_TOOLCHAIN}/ar export RANLIB=${YOUR_TOOLCHAIN}/ranlib export LD=${YOUR_TOOLCHAIN}/ld export CFLAGS="-arch armv7s -std=c99 -miphoneos-version-min=7.0 -isysroot ${sysroot}" export CXXFLAGS="-arch armv7s -fexceptions -frtti -fpic -std=c++11 -stdlib=libc++ -miphoneos-version-min=7.0 -O2 -g -fvisibility=hidden -fvisibility-inlines-hidden -Os -isysroot ${sysroot}" export LDFLAGS="-stdlib=libc++" boost_include="`cd "../boost_1_62_0/${BUILD_DIR}/include";pwd`" boost_lib="`cd "../boost_1_62_0/${BUILD_DIR}/armv7s/lib";pwd`" make distclean ./configure --prefix=/armv7s --host=arm-apple-darwin10 --with-sysroot=${sysroot} --with-boost-include=${boost_include} --with-boost-lib=${boost_lib} --enable-static --enable-shared=no --disable-examples make -j6 make install DESTDIR=${YOUR_PATH_TO_INSTALL_DIR} armv7sEOF #Create the following shell script for arm64 cat > ~/Downloads/QuantLib-${QL_VERSION}/build-arm64.sh << 'arm64EOF' #!/bin/bash # build-arm64.sh BUILD_DIR=ios-build mkdir -p ${BUILD_DIR} INSTALL_DIR="`pwd`/${BUILD_DIR}" YOUR_PATH_TO_INSTALL_DIR="`cd "${INSTALL_DIR}";pwd`" XCODE_ROOT=/Applications/Xcode.app/Contents/Developer YOUR_TOOLCHAIN=${XCODE_ROOT}/Toolchains/XcodeDefault.xctoolchain/usr/bin # xcrun --sdk iphoneos --show-sdk-path # sysroot=${XCODE_ROOT}/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk sysroot="`xcrun --sdk iphoneos --show-sdk-path`" export CC=${YOUR_TOOLCHAIN}/clang export CXX=${YOUR_TOOLCHAIN}/clang++ export CPP=${YOUR_TOOLCHAIN}/cpp export AR=${YOUR_TOOLCHAIN}/ar export RANLIB=${YOUR_TOOLCHAIN}/ranlib export LD=${YOUR_TOOLCHAIN}/ld export CFLAGS="-arch arm64 -std=c99 -miphoneos-version-min=7.0 -isysroot ${sysroot}" export CXXFLAGS="-arch arm64 -fexceptions -frtti -fpic -std=c++11 -stdlib=libc++ -miphoneos-version-min=7.0 -O2 -g -fvisibility=hidden -fvisibility-inlines-hidden -Os -isysroot ${sysroot}" export LDFLAGS="-stdlib=libc++" boost_include="`cd "../boost_1_62_0/${BUILD_DIR}/include";pwd`" boost_lib="`cd "../boost_1_62_0/${BUILD_DIR}/arm64/lib";pwd`" make distclean ./configure --prefix=/arm64 --host=arm-apple-darwin10 --with-sysroot=${sysroot} --with-boost-include=${boost_include} --with-boost-lib=${boost_lib} --enable-static --enable-shared=no --disable-examples make -j6 make install DESTDIR=${YOUR_PATH_TO_INSTALL_DIR} arm64EOF #Create the following shell script for i386 cat > ~/Downloads/QuantLib-${QL_VERSION}/build-i386.sh << 'i386EOF' #!/bin/bash # build-i386.sh BUILD_DIR=ios-build mkdir -p ${BUILD_DIR} INSTALL_DIR="`pwd`/${BUILD_DIR}" YOUR_PATH_TO_INSTALL_DIR="`cd "${INSTALL_DIR}";pwd`" XCODE_ROOT=/Applications/Xcode.app/Contents/Developer YOUR_TOOLCHAIN=${XCODE_ROOT}/Toolchains/XcodeDefault.xctoolchain/usr/bin # xcrun --sdk iphonesimulator --show-sdk-path # sysroot=${XCODE_ROOT}/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk sysroot="`xcrun --sdk iphonesimulator --show-sdk-path`" export CC=${YOUR_TOOLCHAIN}/clang export CXX=${YOUR_TOOLCHAIN}/clang++ export CPP=${YOUR_TOOLCHAIN}/cpp export AR=${YOUR_TOOLCHAIN}/ar export RANLIB=${YOUR_TOOLCHAIN}/ranlib export LD=${YOUR_TOOLCHAIN}/ld export CFLAGS="-arch i386 -std=c99 -miphoneos-version-min=7.0 -isysroot ${sysroot}" export CXXFLAGS="-arch i386 -fexceptions -frtti -fpic -std=c++11 -stdlib=libc++ -miphoneos-version-min=7.0 -O2 -g -fvisibility=hidden -fvisibility-inlines-hidden -Os -isysroot ${sysroot}" export LDFLAGS="-stdlib=libc++" boost_include="`cd "../boost_1_62_0/${BUILD_DIR}/include";pwd`" boost_lib="`cd "../boost_1_62_0/${BUILD_DIR}/i386/lib";pwd`" make distclean ./configure --prefix=/i386 --host=i386-apple-darwin10 --with-sysroot=${sysroot} --with-boost-include=${boost_include} --with-boost-lib=${boost_lib} --enable-static --enable-shared=no --disable-examples make -j6 make install DESTDIR=${YOUR_PATH_TO_INSTALL_DIR} i386EOF #Create the following shell script for x86_64 cat > ~/Downloads/QuantLib-${QL_VERSION}/build-x86_64.sh << 'x86_64EOF' #!/bin/bash # build-x86_64.sh BUILD_DIR=ios-build mkdir -p ${BUILD_DIR} INSTALL_DIR="`pwd`/${BUILD_DIR}" YOUR_PATH_TO_INSTALL_DIR="`cd "${INSTALL_DIR}";pwd`" XCODE_ROOT=/Applications/Xcode.app/Contents/Developer YOUR_TOOLCHAIN=${XCODE_ROOT}/Toolchains/XcodeDefault.xctoolchain/usr/bin # xcrun --sdk iphonesimulator --show-sdk-path # sysroot=${XCODE_ROOT}/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk sysroot="`xcrun --sdk iphonesimulator --show-sdk-path`" export CC=${YOUR_TOOLCHAIN}/clang export CXX=${YOUR_TOOLCHAIN}/clang++ export CPP=${YOUR_TOOLCHAIN}/cpp export AR=${YOUR_TOOLCHAIN}/ar export RANLIB=${YOUR_TOOLCHAIN}/ranlib export LD=${YOUR_TOOLCHAIN}/ld export CFLAGS="-arch x86_64 -std=c99 -miphoneos-version-min=7.0 -isysroot ${sysroot}" export CXXFLAGS="-arch x86_64 -fexceptions -frtti -fpic -std=c++11 -stdlib=libc++ -miphoneos-version-min=7.0 -O2 -g -fvisibility=hidden -fvisibility-inlines-hidden -Os -isysroot ${sysroot}" export LDFLAGS="-stdlib=libc++" boost_include="`cd "../boost_1_62_0/${BUILD_DIR}/include";pwd`" boost_lib="`cd "../boost_1_62_0/${BUILD_DIR}/x86_64/lib";pwd`" make distclean ./configure --prefix=/x86_64 --host=x86_64-apple-darwin10 --with-sysroot=${sysroot} --with-boost-include=${boost_include} --with-boost-lib=${boost_lib} --enable-static --enable-shared=no --disable-examples make -j6 make install DESTDIR=${YOUR_PATH_TO_INSTALL_DIR} x86_64EOF #run shell scripts to clean and build for iOS cd $HOME/Downloads cd QuantLib-${QL_VERSION} rm -fr ios-build sh build-armv7.sh sh build-armv7s.sh sh build-arm64.sh sh build-i386.sh sh build-x86_64.sh # create FAT libQuantLib.a archive cd $HOME/Downloads/QuantLib-${QL_VERSION} cd ios-build mkdir -p lib /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo \ -arch armv7 "armv7/lib/libQuantLib.a" \ -arch armv7s "armv7s/lib/libQuantLib.a" \ -arch arm64 "arm64/lib/libQuantLib.a" \ -arch i386 "i386/lib/libQuantLib.a" \ -arch x86_64 "x86_64/lib/libQuantLib.a" \ -output "lib/libQuantLib.a" \ -create # create link for include folder mkdir include cd include ln -s ../arm64/include/ql .


(6) Create QuantLib Framework for iOS
shell script    Select all
export QL_VERSION=1.12 cd $HOME/Downloads/QuantLib-${QL_VERSION} cd ios-build #Create the following shell script for build-qlframework.sh cat > build-qlframework.sh << 'qlFrameworkEOF' #!/bin/bash # build-qlframework.sh VERSION_TYPE=Alpha FRAMEWORK_NAME=ql FRAMEWORK_VERSION=A FRAMEWORKDIR=. FRAMEWORK_CURRENT_VERSION=${QL_VERSION} FRAMEWORK_COMPATIBILITY_VERSION=${QL_VERSION} FRAMEWORK_BUNDLE=${FRAMEWORKDIR}/${FRAMEWORK_NAME}.framework rm -rf ${FRAMEWORK_BUNDLE} mkdir -p ${FRAMEWORK_BUNDLE} mkdir -p ${FRAMEWORK_BUNDLE}/Versions mkdir -p ${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION} mkdir -p ${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION}/Resources mkdir -p ${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION}/Headers mkdir -p ${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION}/Documentation ln -s $FRAMEWORK_VERSION ${FRAMEWORK_BUNDLE}/Versions/Current ln -s Versions/Current/Headers ${FRAMEWORK_BUNDLE}/Headers ln -s Versions/Current/Resources ${FRAMEWORK_BUNDLE}/Resources ln -s Versions/Current/Documentation ${FRAMEWORK_BUNDLE}/Documentation ln -s Versions/Current/$FRAMEWORK_NAME ${FRAMEWORK_BUNDLE}/$FRAMEWORK_NAME FRAMEWORK_INSTALL_NAME=${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION}/${FRAMEWORK_NAME} echo "Framework: Lipoing library into ${FRAMEWORK_INSTALL_NAME}" /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo \ -arch armv7 "armv7/lib/libQuantLib.a" \ -arch armv7s "armv7s/lib/libQuantLib.a" \ -arch arm64 "arm64/lib/libQuantLib.a" \ -arch i386 "i386/lib/libQuantLib.a" \ -arch x86_64 "x86_64/lib/libQuantLib.a" \ -output "${FRAMEWORK_INSTALL_NAME}" \ -create echo "Framework: Copying includes..." cp -r include/${FRAMEWORK_NAME}/* ${FRAMEWORK_BUNDLE}/Headers/ cat > ${FRAMEWORK_BUNDLE}/Resources/Info.plist <<InfoplistEOF <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CFBundleDevelopmentRegion</key> <string>English</string> <key>CFBundleExecutable</key> <string>${FRAMEWORK_NAME}</string> <key>CFBundleIdentifier</key> <string>quantlib.org</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundlePackageType</key> <string>FMWK</string> <key>CFBundleSignature</key> <string>????</string> <key>CFBundleVersion</key> <string>${FRAMEWORK_CURRENT_VERSION}</string> </dict> </plist> InfoplistEOF echo "Done: ${FRAMEWORK_BUNDLE}" qlFrameworkEOF # build framework sh build-qlframework.sh


(7) Test build x86_64 for iOS frameworks with
shell script    Select all
#Create testql.cpp cd $HOME/Downloads cat > testql.cpp << 'testqlEOF' #include <ql/quantlib.hpp> int main() { std::cout << "BOOST version is " << BOOST_VERSION << std::endl; std::cout << "QL version is " << QL_VERSION << std::endl; #if __x86_64__ || __WORDSIZE == 64 std::cout << "This is 64 bits" << std::endl; #elif __i386__ || __WORDSIZE == 32 std::cout << "This is 32 bits" << std::endl; #else std::cout << "This is something else" << std::endl; #endif return 0; } testqlEOF # test build example using iOS frameworks xcrun clang++ -std=c++11 -stdlib=libc++ -F $HOME/Downloads/QuantLib-1.12/ios-build -F $HOME/Downloads/boost_1_62_0/ios-build -framework ql -isysroot `xcrun --sdk iphonesimulator --show-sdk-path` -arch x86_64 -miphoneos-version-min=7.0 -fvisibility=hidden -o testql testql.cpp ./testql


(8) Build frameworks for macosx and build RQuantLib package from source
shell script    Select all
cd $HOME/Downloads/boost_1_62_0 # Append the following user-config.jam at ~/Downloads/boost_1_62_0/tools/build/src/ cat >> tools/build/src/user-config.jam << 'EOF' # ------------------ # darwin macosx configuration. # ------------------ local XCODE_ROOT = /Applications/Xcode.app/Contents/Developer ; # xcrun --sdk macosx --show-sdk-path local MACOSXSDK = MacOSX.sdk ; using darwin : i386~macosx : $(XCODE_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ : <striper>$(XCODE_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/strip <root>$(XCODE_ROOT)/Platforms/MacOSX.platform/Developer/SDKs/$(MACOSXSDK) <compileflags>"-arch i386" <compileflags>"-isysroot $(XCODE_ROOT)/Platforms/MacOSX.platform/Developer/SDKs/$(MACOSXSDK)" <compileflags>-fexceptions <compileflags>-frtti <compileflags>-fpic <compileflags>-DBOOST_SP_USE_SPINLOCK <compileflags>-std=c++11 <compileflags>-stdlib=libc++ <compileflags>-mmacosx-version-min=10.9 <compileflags>-O2 <compileflags>-g <compileflags>-Os <compileflags>-Wno-long-long <compileflags>-Wno-missing-field-initializers <compileflags>-Wno-unused-variable <linkflags>-stdlib=libc++ <linkflags>-mmacosx-version-min=10.9 <architecture>x86 <address-model>32 <target-os>darwin ; using darwin : x86_64~macosx : $(XCODE_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ : <striper>$(XCODE_ROOT)/Toolchains/XcodeDefault.xctoolchain/usr/bin/strip <root>$(XCODE_ROOT)/Platforms/MacOSX.platform/Developer/SDKs/$(MACOSXSDK) <compileflags>"-arch x86_64" <compileflags>"-isysroot $(XCODE_ROOT)/Platforms/MacOSX.platform/Developer/SDKs/$(MACOSXSDK)" <compileflags>-fexceptions <compileflags>-frtti <compileflags>-fpic <compileflags>-DBOOST_SP_USE_SPINLOCK <compileflags>-std=c++11 <compileflags>-stdlib=libc++ <compileflags>-mmacosx-version-min=10.9 <compileflags>-O2 <compileflags>-g <compileflags>-Os <compileflags>-Wno-long-long <compileflags>-Wno-missing-field-initializers <compileflags>-Wno-unused-variable <linkflags>-stdlib=libc++ <linkflags>-mmacosx-version-min=10.9 <architecture>x86 <address-model>64 <target-os>darwin ; EOF # build boost libraries for macosx ./bootstrap.sh --with-libraries=atomic,chrono,date_time,exception,filesystem,graph,iostreams,math,program_options,random,regex,serialization,signals,system,test,thread,wave # clean and build for macosx, j option is num of cores x 1.5 rm -fr macosx-build ./b2 -j6 --build-dir=macosx-build --stagedir=macosx-build/i386 toolset=darwin-i386~macosx architecture=x86 address-model=32 target-os=darwin threading=multi link=static stage ./b2 -j6 --build-dir=macosx-build --stagedir=macosx-build/x86_64 toolset=darwin-x86_64~macosx architecture=x86 address-model=64 target-os=darwin threading=multi link=static stage # create libboost.a archive for each architecture cd $HOME/Downloads/boost_1_62_0/macosx-build xcrun --sdk macosx ar crus i386/libboost.a boost/bin.v2/libs/*/build/darwin-i386~macosx/release/link-static/threading-multi/*/*.o xcrun --sdk macosx ar crus x86_64/libboost.a boost/bin.v2/libs/*/build/darwin-x86_64~macosx/release/link-static/threading-multi/*/*.o # create FAT libboost.a archive mkdir -p lib /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo \ -arch i386 "i386/libboost.a" \ -arch x86_64 "x86_64/libboost.a" \ -output "lib/libboost.a" \ -create # create link for include folder mkdir include cd include ln -s ../../boost . # build Boost Framework for macosx #Create the following shell script for build-boostframework.sh cd $HOME/Downloads/boost_1_62_0/macosx-build cat > build-boostframework.sh << 'boostFrameworkEOF' #!/bin/bash # build-boostframework.sh VERSION_TYPE=Alpha FRAMEWORK_NAME=boost FRAMEWORK_VERSION=A FRAMEWORKDIR=. FRAMEWORK_CURRENT_VERSION=1.62.0 FRAMEWORK_COMPATIBILITY_VERSION=1.62.0 FRAMEWORK_BUNDLE=${FRAMEWORKDIR}/${FRAMEWORK_NAME}.framework rm -rf ${FRAMEWORK_BUNDLE} mkdir -p ${FRAMEWORK_BUNDLE} mkdir -p ${FRAMEWORK_BUNDLE}/Versions mkdir -p ${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION} mkdir -p ${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION}/Resources mkdir -p ${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION}/Headers mkdir -p ${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION}/Documentation ln -s $FRAMEWORK_VERSION ${FRAMEWORK_BUNDLE}/Versions/Current ln -s Versions/Current/Headers ${FRAMEWORK_BUNDLE}/Headers ln -s Versions/Current/Resources ${FRAMEWORK_BUNDLE}/Resources ln -s Versions/Current/Documentation ${FRAMEWORK_BUNDLE}/Documentation ln -s Versions/Current/$FRAMEWORK_NAME ${FRAMEWORK_BUNDLE}/$FRAMEWORK_NAME FRAMEWORK_INSTALL_NAME=${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION}/${FRAMEWORK_NAME} echo "Framework: Lipoing library into ${FRAMEWORK_INSTALL_NAME}" /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo \ -arch i386 "i386/libboost.a" \ -arch x86_64 "x86_64/libboost.a" \ -output "${FRAMEWORK_INSTALL_NAME}" \ -create echo "Framework: Copying includes..." cp -r include/${FRAMEWORK_NAME}/* ${FRAMEWORK_BUNDLE}/Headers/ cat > ${FRAMEWORK_BUNDLE}/Resources/Info.plist <<InfoplistEOF <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CFBundleDevelopmentRegion</key> <string>English</string> <key>CFBundleExecutable</key> <string>${FRAMEWORK_NAME}</string> <key>CFBundleIdentifier</key> <string>${FRAMEWORK_NAME}.org</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundlePackageType</key> <string>FMWK</string> <key>CFBundleSignature</key> <string>????</string> <key>CFBundleVersion</key> <string>${FRAMEWORK_CURRENT_VERSION}</string> </dict> </plist> InfoplistEOF echo "Done: ${FRAMEWORK_BUNDLE}" boostFrameworkEOF # build framework sh build-boostframework.sh # build QuantLib libraries for macosx export QL_VERSION=1.12 cd $HOME/Downloads/QuantLib-${QL_VERSION} #Create the following shell script for i386 cat > ~/Downloads/QuantLib-${QL_VERSION}/build-i386-macosx.sh << 'i386EOF' #!/bin/bash # build-i386-macosx.sh BUILD_DIR=macosx-build mkdir -p ${BUILD_DIR} INSTALL_DIR="`pwd`/${BUILD_DIR}" YOUR_PATH_TO_INSTALL_DIR="`cd "${INSTALL_DIR}";pwd`" XCODE_ROOT=/Applications/Xcode.app/Contents/Developer YOUR_TOOLCHAIN=${XCODE_ROOT}/Toolchains/XcodeDefault.xctoolchain/usr/bin # xcrun --sdk macosx --show-sdk-path # sysroot=${XCODE_ROOT}/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk sysroot="`xcrun --sdk macosx --show-sdk-path`" export CC=${YOUR_TOOLCHAIN}/clang export CXX=${YOUR_TOOLCHAIN}/clang++ export CPP=${YOUR_TOOLCHAIN}/cpp export AR=${YOUR_TOOLCHAIN}/ar export RANLIB=${YOUR_TOOLCHAIN}/ranlib export LD=${YOUR_TOOLCHAIN}/ld export CFLAGS="-arch i386 -std=c99 -mmacosx-version-min=10.7 -isysroot ${sysroot}" export CXXFLAGS="-arch i386 -fexceptions -frtti -fpic -std=c++11 -stdlib=libc++ -mmacosx-version-min=10.9 -O2 -g -Os -isysroot ${sysroot}" export LDFLAGS="-stdlib=libc++ -mmacosx-version-min=10.9" boost_include="`cd "../boost_1_62_0/${BUILD_DIR}/include";pwd`" boost_lib="`cd "../boost_1_62_0/${BUILD_DIR}/i386/lib";pwd`" make distclean ./configure --prefix=/i386 --host=i386-apple-darwin10 --with-sysroot=${sysroot} --with-boost-include=${boost_include} --with-boost-lib=${boost_lib} --enable-intraday --enable-static --enable-shared=no --disable-examples make -j6 make install DESTDIR=${YOUR_PATH_TO_INSTALL_DIR} i386EOF #Create the following shell script for x86_64 cat > ~/Downloads/QuantLib-${QL_VERSION}/build-x86_64-macosx.sh << 'x86_64EOF' #!/bin/bash # build-x86_64-macosx.sh BUILD_DIR=macosx-build mkdir -p ${BUILD_DIR} INSTALL_DIR="`pwd`/${BUILD_DIR}" YOUR_PATH_TO_INSTALL_DIR="`cd "${INSTALL_DIR}";pwd`" XCODE_ROOT=/Applications/Xcode.app/Contents/Developer YOUR_TOOLCHAIN=${XCODE_ROOT}/Toolchains/XcodeDefault.xctoolchain/usr/bin # xcrun --sdk macosx --show-sdk-path # sysroot=${XCODE_ROOT}/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk sysroot="`xcrun --sdk macosx --show-sdk-path`" export CC=${YOUR_TOOLCHAIN}/clang export CXX=${YOUR_TOOLCHAIN}/clang++ export CPP=${YOUR_TOOLCHAIN}/cpp export AR=${YOUR_TOOLCHAIN}/ar export RANLIB=${YOUR_TOOLCHAIN}/ranlib export LD=${YOUR_TOOLCHAIN}/ld export CFLAGS="-arch x86_64 -std=c99 -mmacosx-version-min=10.7 -isysroot ${sysroot}" export CXXFLAGS="-arch x86_64 -fexceptions -frtti -fpic -std=c++11 -stdlib=libc++ -mmacosx-version-min=10.9 -O2 -g -Os -isysroot ${sysroot}" export LDFLAGS="-stdlib=libc++ -mmacosx-version-min=10.9" boost_include="`cd "../boost_1_62_0/${BUILD_DIR}/include";pwd`" boost_lib="`cd "../boost_1_62_0/${BUILD_DIR}/x86_64/lib";pwd`" make distclean ./configure --prefix=/x86_64 --host=x86_64-apple-darwin10 --with-sysroot=${sysroot} --with-boost-include=${boost_include} --with-boost-lib=${boost_lib} --enable-intraday --enable-static --enable-shared=no --disable-examples make -j6 make install DESTDIR=${YOUR_PATH_TO_INSTALL_DIR} x86_64EOF #run shell scripts to clean and build for macosx cd $HOME/Downloads/QuantLib-${QL_VERSION} rm -fr macosx-build sh build-i386-macosx.sh sh build-x86_64-macosx.sh # create FAT libQuantLib.a archive cd $HOME/Downloads/QuantLib-${QL_VERSION} cd macosx-build mkdir -p lib /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo \ -arch i386 "i386/lib/libQuantLib.a" \ -arch x86_64 "x86_64/lib/libQuantLib.a" \ -output "lib/libQuantLib.a" \ -create # create link for include folder mkdir include cd include ln -s ../x86_64/include/ql . cd $HOME/Downloads/QuantLib-${QL_VERSION}/macosx-build # build QuantLib Framework for macosx #Create the following shell script for build-qlframework.sh cat > build-qlframework.sh << 'qlFrameworkEOF' #!/bin/bash # build-qlframework.sh VERSION_TYPE=Alpha FRAMEWORK_NAME=ql FRAMEWORK_VERSION=A FRAMEWORKDIR=. FRAMEWORK_CURRENT_VERSION=${QL_VERSION} FRAMEWORK_COMPATIBILITY_VERSION=${QL_VERSION} FRAMEWORK_BUNDLE=${FRAMEWORKDIR}/${FRAMEWORK_NAME}.framework rm -rf ${FRAMEWORK_BUNDLE} mkdir -p ${FRAMEWORK_BUNDLE} mkdir -p ${FRAMEWORK_BUNDLE}/Versions mkdir -p ${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION} mkdir -p ${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION}/Resources mkdir -p ${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION}/Headers mkdir -p ${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION}/Documentation ln -s $FRAMEWORK_VERSION ${FRAMEWORK_BUNDLE}/Versions/Current ln -s Versions/Current/Headers ${FRAMEWORK_BUNDLE}/Headers ln -s Versions/Current/Resources ${FRAMEWORK_BUNDLE}/Resources ln -s Versions/Current/Documentation ${FRAMEWORK_BUNDLE}/Documentation ln -s Versions/Current/$FRAMEWORK_NAME ${FRAMEWORK_BUNDLE}/$FRAMEWORK_NAME FRAMEWORK_INSTALL_NAME=${FRAMEWORK_BUNDLE}/Versions/${FRAMEWORK_VERSION}/${FRAMEWORK_NAME} echo "Framework: Lipoing library into ${FRAMEWORK_INSTALL_NAME}" /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo \ -arch i386 "i386/lib/libQuantLib.a" \ -arch x86_64 "x86_64/lib/libQuantLib.a" \ -output "${FRAMEWORK_INSTALL_NAME}" \ -create echo "Framework: Copying includes..." cp -r include/${FRAMEWORK_NAME}/* ${FRAMEWORK_BUNDLE}/Headers/ cat > ${FRAMEWORK_BUNDLE}/Resources/Info.plist <<InfoplistEOF <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>CFBundleDevelopmentRegion</key> <string>English</string> <key>CFBundleExecutable</key> <string>${FRAMEWORK_NAME}</string> <key>CFBundleIdentifier</key> <string>quantlib.org</string> <key>CFBundleInfoDictionaryVersion</key> <string>6.0</string> <key>CFBundlePackageType</key> <string>FMWK</string> <key>CFBundleSignature</key> <string>????</string> <key>CFBundleVersion</key> <string>${FRAMEWORK_CURRENT_VERSION}</string> </dict> </plist> InfoplistEOF echo "Done: ${FRAMEWORK_BUNDLE}" qlFrameworkEOF # build framework sh build-qlframework.sh # Test build example using macosx frameworks clang++ -std=c++11 -stdlib=libc++ -F $HOME/Downloads/QuantLib-1.12/macosx-build -F $HOME/Downloads/boost_1_62_0/macosx-build -framework ql -arch x86_64 -o testql testql.cpp ./testql # Build RQuantLib for Mac OSX 10.12.6 (1) Build the above macosx x86_64 libraries for QuantLib and Boost. For Quantlib, it should be version >=1.8 and with --enable-intraday option. For Boost and QuantLib, should remove -fvisibility=hidden -fvisibility-inlines-hidden (2) Install R 3.4.4 from https://cran.r-project.org/bin/macosx/ and optionally install RStudio (3) Install clang 4.0 from http://r.research.att.com/libs/ sudo tar xzvf ${HOME}/Downloads/clang-4.0.0-darwin15.6-Release.tar.gz -C / (4) Create R/Makevars mkdir -p ${HOME}/.R cat > ${HOME}/.R/Makevars << 'EOF' CC=/usr/local/clang4/bin/clang CXX=/usr/local/clang4/bin/clang++ CXXFLAGS=-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -Wno-nonportable-include-path -Wno-unused-variable -I${HOME}/Downloads/QuantLib-1.12/macosx-build/x86_64/include LDFLAGS=-L/usr/local/clang4/lib -L${HOME}/Downloads/QuantLib-1.12/macosx-build/x86_64/lib EOF # if compile using clang6 under the latest R 3.5.0, should change rge path to /usr/local/clang6 accordingly. (5) Install QuantLib 1.12 to /usr/local cd ${HOME}/Downloads/QuantLib-1.12/macosx-build/x86_64; tar czvf ../ql-1.12.tgz bin include lib share cd ${HOME}/Downloads/QuantLib-1.12/macosx-build sudo tar xzvf ql-1.12.tgz -C /usr/local (6) Build RQuantLib with R terminal session from source and install R install.packages("RQuantLib", configure.args = "--with-boost-include=${HOME}/Downloads/boost_1_62_0/macosx-build/include --with-boost-lib=${HOME}/Downloads/boost_1_62_0/macosx-build/x86_64/lib", type = "source") # installing to /Library/Frameworks/R.framework/Versions/3.4/Resources/library/RQuantLib/libs # build binary package (should install dependencies Rcpp and zoo first) using the following # curl -O https://cran.cnr.berkeley.edu/src/contrib/RQuantLib_0.4.4.tar.gz # R CMD INSTALL --build --configure-args="--with-boost-include=${HOME}/Downloads/boost_1_62_0/macosx-build/include --with-boost-lib=${HOME}/Downloads/boost_1_62_0/macosx-build/x86_64/lib" RQuantLib_0.4.4.tar.gz (7) Optionally, install https://www.xquartz.org/ if want to remove warning on : unable to load shared object '/Library/Frameworks/R.framework/Resources/modules//R_X11.so' # Install dependencies for testing RQuantLib R install.packages(c("Rcpp","zoo")) install.packages(c("rgl","RUnit"))


(9) Test RQuantLib binary package for R version 3.4.4 on Mac OSX 10.12.6
bond1.R  Select all
#Simple call with a flat curve require(RQuantLib) bond <- list(faceAmount=100, settlementDays=1, issueDate=as.Date("2014-11-30"), redemption=100)
scheduleparams <- list(effectiveDate=as.Date("2014-11-30"), maturityDate=as.Date("2018-11-30"), period=2, calendar="UnitedStates", businessDayConvention = 4, terminationDateConvention=4, dateGeneration=1, endOfMonth=1, dayCounter = 'Thirty360')
coupon.rate <- c(0.02875)
params <- list(tradeDate=as.Date('2014-2-15'), settleDate=as.Date('2014-2-19'), dt=.25, interpWhat="discount", interpHow="loglinear")
setEvaluationDate(as.Date("2014-11-22"))
discountCurve.flat <- DiscountCurve(params, list(flat=0.05))
FixedRateBond(bond=bond, rates=coupon.rate, schedule=scheduleparams, calc=list(dayCounter='ActualActual.ISMA',compounding='Compounded', freq='Annual', durationType='Modified'),discountCurve=discountCurve.flat)

FixedRateBondPriceByYield( settlementDays=1, yield=0.05127, faceAmount=100, effectiveDate=as.Date("2014-11-30"), maturityDate=as.Date("2018-11-30"), period=2, calendar="UnitedStates", rates=c(0.05), dayCounter=2, businessDayConvention=4, compound=0, redemption=100, issueDate=as.Date("2014-11-30"))

FixedRateBondYield( settlementDays=1, price=99.54593, faceAmount=100, effectiveDate=as.Date("2014-11-30"), maturityDate=as.Date("2018-11-30"), period=2, calendar="UnitedStates", rates=c(0.05), dayCounter=2, businessDayConvention=4, compound=0, redemption=100, issueDate=as.Date("2014-11-30"))