code style

2019年8月2日 星期五

u-boot arm64 from QEMU

#!/bin/bash
PREFIX="aarch64-linux-gnu-"
arch="arm64"
TOP=`pwd`

UBOOT_SRC_URL="https://ftp.denx.de/pub/u-boot/u-boot-2018.11.tar.bz2"
TOOLCHAIN_SRC_URL="http://releases.linaro.org/components/toolchain/binaries/6.5-2018.12/aarch64-linux-gnu/gcc-linaro-6.5.0-2018.12-x86_64_aarch64-linux-gnu.tar.xz"
BUSYBOX_SRC_URL="http://busybox.net/downloads/busybox-1.23.2.tar.bz2"
KERNEL_SRC_URL="https://www.kernel.org/pub/linux/kernel/v4.x/linux-4.4.8.tar.xz"

UBOOT_DIR="uboot_src"
TOOLCHAIN_DIR="toolchain_src"
BUSYBOX_DIR="busybox_src"
KERNEL_DIR="linux_src"

TOOLCHAIN_PATH="$TOP/$TOOLCHAIN_DIR/bin"

BUSYBOX_PALTFORM="busybox-$arch"
LINUX_PALTFORM="linux-$arch-basic"



download_src(){
if [ -d "$1" ]; then
echo "The $1 has been downloaded"
else
mkdir "$1"
wget -P /tmp/ "$2" && tar $3 "/tmp/`basename $2`" -C "$1" --strip-components=1
fi
}

toolchain() {
download_src $TOOLCHAIN_DIR $TOOLCHAIN_SRC_URL xJf
export PATH=${PATH}:${TOOLCHAIN_PATH}
export ARCH=$arch
export CROSS_COMPILE=$PREFIX
}


uboot() {
download_src $UBOOT_DIR $UBOOT_SRC_URL jxvf
toolchain;
cd "$UBOOT_DIR"
make qemu_arm64_defconfig
make -j4
}


busybox() {
download_src $BUSYBOX_DIR $BUSYBOX_SRC_URL xjf
toolchain;
mkdir -pv $TOP/initramfs
mkdir -pv $TOP/obj/$BUSYBOX_PALTFORM

cd "$BUSYBOX_DIR"
    make mrproper
    make O=$TOP/obj/$BUSYBOX_PALTFORM defconfig
sed -i 's/.*CONFIG_STATIC.*/CONFIG_STATIC=y/' $TOP/obj/$BUSYBOX_PALTFORM/.config
sed -i 's/.*CONFIG_INSTALL_NO_USR.*/CONFIG_INSTALL_NO_USR=y/' $TOP/obj/$BUSYBOX_PALTFORM/.config
sed -i 's/.*CONFIG_FEATURE_PREFER_APPLETS.*/CONFIG_FEATURE_PREFER_APPLETS=y/' $TOP/obj/$BUSYBOX_PALTFORM/.config
sed -i 's/.*CONFIG_FEATURE_SH_STANDALONE.*/CONFIG_FEATURE_SH_STANDALONE=y/' $TOP/obj/$BUSYBOX_PALTFORM/.config
    make clean
    make O=$TOP/obj/$BUSYBOX_PALTFORM -j12 2>&1 | tee  ../busybox_build.log
    make O=$TOP/obj/$BUSYBOX_PALTFORM install

# initramfs
cd $TOP/initramfs
mkdir -pv {bin,sbin,etc/init.d,proc,sys,dev,lib,usr/{bin,sbin}}
cp -av $TOP/obj/$BUSYBOX_PALTFORM/_install/* .

cd dev
sudo mknod -m 660 null c 1 3
sudo mknod -m 622 console c 5 1
sudo mknod -m 622 tty0 c 4 0
ln -sf null tty2
ln -sf null tty3
ln -sf null tty4
cd ..
    #cp -a $TOP/toolchain_src/arm-linux-gnueabihf/libc/* lib

#don't have any space or tab when use EOF to output file
cat << EOF > etc/init.d/rcS
#!/bin/sh
mount -t proc none /proc
mount -t sysfs none /sys
echo -e "\nBoot took $(cut -d" " -f1 /proc/uptime) seconds\n"
EOF

    chmod 755 etc/init.d/rcS
    find . -print0 \
        | cpio --null -ov --format=newc \
        | gzip -9 > $TOP/obj/$BUSYBOX_PALTFORM/initramfs-"$BUSYBOX_PALTFORM".cpio.gz
}

initramfs() {
    TOP=`pwd`
    cd $TOP/initramfs/"$BUSYBOX_PALTFORM"

    #find ./ | cpio -o -H newc | gzip > $TOP/obj/initramfs-$BUSYBOX_PALTFORM.cpio.gz

    find . -print0 \
    | cpio --null -ov --format=newc \
    | gzip -9 > $TOP/obj/BUSYBOX_PALTFORM/initramfs-"$BUSYBOX_PALTFORM".cpio.gz
}

kernel() {
download_src $KERNEL_DIR $KERNEL_SRC_URL xJf
toolchain;
# build kernel
cd $TOP
    mkdir -pv obj/"$LINUX_PALTFORM"
    make -C $KERNEL_DIR O=../obj/$LINUX_PALTFORM mrproper
    make  -C $KERNEL_DIR O=../obj/$LINUX_PALTFORM  defconfig
    sed -i 's/.*CONFIG_EXPERIMENTAL.*/CONFIG_EXPERIMENTAL=y/' ../obj/$LINUX_PALTFORM/.config
sed -i 's/.*CONFIG_DEBUG_INFO.*/CONFIG_DEBUG_INFO=y/' ../obj/$LINUX_PALTFORM/.config
sed -i 's/.*CONFIG_KGDB.*/CONFIG_KGDB=y/' ../obj/$LINUX_PALTFORM/.config
sed -i 's/.*CONFIG_KGDB_LOW_LEVEL_TRAP.*/CONFIG_KGDB_LOW_LEVEL_TRAP=y/' ../obj/$LINUX_PALTFORM/.config
sed -i 's/.*CONFIG_FRAME_POINTER.*/CONFIG_FRAME_POINTER=y/' ../obj/$LINUX_PALTFORM/.config
sed -i 's/.*CONFIG_MAGIC_SYSRQ.*/CONFIG_MAGIC_SYSRQ=y/' ../obj/$LINUX_PALTFORM/.config
sed -i 's/.*CONFIG_8139CP.*/CONFIG_8139CP=y/' ../obj/$LINUX_PALTFORM/.config
sed -i 's/.*CONFIG_DEBUG_SET_MODULE_RONX.*/CONFIG_DEBUG_SET_MODULE_RONX=n/' obj/$LINUX_PALTFORM/.config
sed -i 's/.*CONFIG_DEBUG_RODATA.*/CONFIG_DEBUG_RODATA=n/' ../obj/$LINUX_PALTFORM/.config
sed -i 's/.*CONFIG_MODULE_FORCE_LOAD.*/CONFIG_MODULE_FORCE_LOAD=y/' ../obj/$LINUX_PALTFORM/.config
sed -i 's/.*CONFIG_MODULE_UNLOAD.*/CONFIG_MODULE_UNLOAD=y/' ../obj/$LINUX_PALTFORM/.config
sed -i 's/.*CONFIG_MODULE_FORCE_UNLOAD.*/CONFIG_MODULE_FORCE_UNLOAD=y/' ../obj/$LINUX_PALTFORM/.config

cat << EOF >> ../obj/"$LINUX_PALTFORM"/.config
CONFIG_KPROBES_ON_FTRACE=y
CONFIG_UPROBES=y
CONFIG_TRACE_IRQFLAGS=y
CONFIG_TRACER_MAX_TRACE=y
CONFIG_RING_BUFFER_ALLOW_SWAP=y
CONFIG_FTRACE=y
CONFIG_FUNCTION_TRACER=y
CONFIG_FUNCTION_GRAPH_TRACER=y
CONFIG_IRQSOFF_TRACER=y
CONFIG_SCHED_TRACER=y
CONFIG_FTRACE_SYSCALLS=y
CONFIG_TRACER_SNAPSHOT=y
CONFIG_TRACER_SNAPSHOT_PER_CPU_SWAP=y
CONFIG_STACK_TRACER=y
CONFIG_UPROBE_EVENT=y
CONFIG_DYNAMIC_FTRACE=y
CONFIG_DYNAMIC_FTRACE_WITH_REGS=y
CONFIG_FUNCTION_PROFILER=y
CONFIG_FTRACE_MCOUNT_RECORD=y
CONFIG_FTRACE_SELFTEST=y
CONFIG_FTRACE_STARTUP_TEST=y
CONFIG_EVENT_TRACE_TEST_SYSCALLS=y
CONFIG_MMIOTRACE=y
CONFIG_PERCPU_RWSEM=y
EOF

yes '' | make -C $KERNEL_DIR O=../obj/$LINUX_PALTFORM oldconfig
make -C $KERNEL_DIR O=../obj/$LINUX_PALTFORM clean
time make -C $KERNEL_DIR O=../obj/$LINUX_PALTFORM -j12 2>&1 | tee kernel_build.log
}


qemu() {
    qemu-system-aarch64 -machine virt -cpu cortex-a53 -nographic -m 2048 \
    -kernel obj/"$LINUX_PALTFORM"/arch/arm64/boot/Image \
    -initrd obj/$BUSYBOX_PALTFORM/initramfs-"$BUSYBOX_PALTFORM".cpio.gz \
    -append "root=/dev/ram rdinit=/sbin/init console=ttyAMA0,115200"
}



initramfs() {
    cd $TOP/initramfs/"$BUSYBOX_PALTFORM"

    #find ./ | cpio -o -H newc | gzip > $TOP/obj/initramfs-$BUSYBOX_PALTFORM.cpio.gz

    find . -print0 \
    | cpio --null -ov --format=newc \
    | gzip -9 > $TOP/obj/$BUSYBOX_PALTFORM/initramfs-"$BUSYBOX_PALTFORM".cpio.gz
}

while true; do
    case "$1" in
-t|--toolchain) toolchain; exit 0;;
-u|--uboot) uboot; exit 0;;
-b|--busybox) busybox; exit 0;;
-i|--initramfs) initramfs; exit 0;;
-k|--kernel) kernel; exit 0;;
        -r|--run) qemu; exit 0;;
        --) shift; break;;
    esac
    shift
done


2019年7月28日 星期日

espressobin boot from UART

ESPRESSObin v7 boot from UART



如果版子上沒有bootloader(u-boot), 可使用UART mode 來recovery.
a. build UART images, they are located in build/a3700/debug/uart-images/ directory:
$ ls build/a3700/debug/uart-images
TIM_ATF.bin  atf-ntim.txt  boot-image_h.bin  wtmi_h.bin

b. 調jump to UART mode, 參考boot modes table


Boot modes - ESPRESSObin v7
ESPRESSObin boot modeJ11J3J10
Serial NOR flash download mode2-32-31-2
eMMC download mode2-31-22-3
eMMC alternate download mode2-31-22-3
SATA download mode2-31-22-3
Serial NAND flash download mode2-31-22-3
UART mode2-31-22-3
MicroSD card2-31-22-3
 c.  open comport from windows and we should  should see the > prompt presented on the HyperTerminal screen. In this terminal, type wtp to switch the BootROM to WTP download mode.
 d.  download the WTPTP tools from extranet. 
 e. run this command from cmd.exe(-C 38 is your COM number)
>> WtpDownload.exe -P UART -C 38 -R 115200 -B path\to\TIM_ATF.bin -I path\to\wtmi_h.bin -I path\to\boot-image_h.bin -E

f. when you see the "Mavell>>" , use tftp to update bootloader
 set ip address

Marvell>> setenv serverip 192.168.x.x
Marvell>> setenv ipaddr 192.168.x.x
check
Marvell>> ping $serverip
update u-boot

Marvell>> bubt espressobin-bootloader-cpu-1000-ddr3-2cs-1g-atf-ga3306ab-uboot-gaee49fc-20180129-REL.bin
Burning U-BOOT image "espressobin-bootloader-cpu-1000-ddr3-2cs-1g-atf-ga3306ab-uboot-gaee49fc-20180129-REL.bin" from "tftp" to "spi"
Using neta@30000 device
TFTP from server 192.168.10.134; our IP address is 192.168.10.145
Filename 'espressobin-bootloader-cpu-1000-ddr3-2cs-1g-atf-ga3306ab-uboot-gaee49fc-20180129-REL.bin'.
Load address: 0x2000000
Loading: #################################################################
 #################################################################
 #################################################################
 #################################################################
 ##########################
 2.3 MiB/s
done
Bytes transferred = 4194304 (400000 hex)
Image checksum...OK!
SF: Detected w25q32dw with page size 256 Bytes, erase size 4 KiB, total 4 MiB
Erasing 4198400 bytes (1025 blocks) at offset 0 ...Done!
Writing 4194304 bytes from 0x2000000 to offset 0 ...Done!
g. chang jump to "Serial NOR flash download mode"

2019年7月15日 星期一

Bootloader

embedded linux system上, software可分4個層次:
1. 引導加載程序:
是系統上電後運行的第一段code。包括在firmware中的boot code(option)和bootloader兩部分
2.kernel:
包括訂製kernel和啟動參數
3.rootfs:
位於flash上的rootfs.通常用ramdisk來作為rootfs.
4.application:
實際上板子上開發應用的程式

x86主機:
引導加載程序由BIOS(位於flash rom)和位於硬碟的MBR中的OS BootLooader組成(GRUB,LILO..)
系統上電後運行BIOS => 硬體檢測,資源分配 => 將MBR中的 BootLooader由硬碟讀到ram=>
將控制權交給OS BootLoader

embedded linux system的boot loader:
而在嵌入式系統中,通常並沒有像BIOS那樣的程式(注,有的嵌入式CPU也會內嵌一段短小的啟動程序),因此整個系統的加載啟動任務就完全由BootLoader來完成.

比如在一個基於ARM7TDMI core的嵌入式系統中,系統在上電或複位時通常都從地址0x00000000處開始執行,而在這個地址處安排的通常就是系統的BootLoader程序.

簡單地說,BootLoader就是在kernel運行之前運行的一段小程序.通過這段小程序,我們可以初始化硬體設備、建立內存空間的映射圖,從而將系統的軟硬體環境帶到一個合適的狀態,以便為最終調用kernel准備好正確的環境.

啟動過程:
可分為Single Stage和Multi-Stage, 一般flash上使用的是Multi-Stage. 大多是stage1和stage2.
stage1:
硬體設備initial
RAM空間 inital
copy BootLoader 的stage2 到RAM空間中
設置好stack point(sp)
跳轉到stage2的 c entry point

stage2:
硬體設備initial
檢測memory map
將kernel和rootfs從flash中讀到RAM
為kernel設置啟動參數
調用kernel


2017年7月17日 星期一

Device Tree(4)Device Node Requirements

一個device tree最少要由一個root node,cpus node, memory node組成。

Root Node
Root node表示符號為"/",下表是它的屬性

Aliases Node
這個屬性在root的位置,node name為aliases。必須要為小寫字元開頭,長度為1~31字元,主要是用來減少冗長的路徑名稱。
可用字元如下圖:






例:
aliases {
  serial0 = "/simple-bus@fe000000/serial@llc500";
  ethernet0 = "/simple-bus@fe000000/ethernet@31c000";
}

就可用serial0來代表/simple-bus@fe000000/serial@llc500

Memory Node
表示實際系統上的memory layout,屬性見下圖:














例:
一個 64-bit Power system 的 physical memory layout:
RAM: starting address 0x0, length 0x80000000 (2GB)
RAM: starting address 0x100000000, length 0x100000000 (4GB)

在root node定義address and size,這裡表示reg 屬性需要2個32-bit cells
#address-cells = <2>;
#size-cells = <2>;

可用下面兩種方式表示這兩段記憶體

(1)
memory@0 {
   device_type = "memory";
   reg = <0x000000000 0x00000000 0x00000000 0x80000000
              0x000000001 0x00000000  0x00000001 0x00000000>;
};

(2)
memory@0 {
device_type = "memory";
reg = <0x000000000 0x00000000 0x00000000 0x80000000>;
};

memory@100000000 {
device_type = "memory";
reg = <0x000000001 0x00000000 0x00000001 0x00000000>;
};

Chosen Node
用來描述由系統firmware指定的runtime parameter。會放在root node底下。原本通過tag list傳遞的一些 kernel的運行時參數可以通過Device Tree傳遞。例如command line可以通過bootargs這個property這個屬性傳遞,node屬性如下圖:















例:
chosen {
   bootargs = “root=/dev/nfs rw nfsroot=192.168.1.1 console=ttyS0,115200”;
};

CPUS Node
表示實際系統上的CPU,因為可能不只一個CPU又或是多核心,cpus node包含cpu node,等下會說到。cpus node屬性如下圖:



CPU Node
表示實際系統上的CPU,屬性如下圖:














































Device Tree(3)Interrupts and Interrupt Mapping

Properties for Interrupt Generating Devices
interrupt-parent 
在device tree中interrupt controller有一個interrupt-parent 屬性,這個用來表示interrupt之間的路由關係,它用一個phandle value表示其parent。各個interrupt controller的定義是不一樣的,有的用3個u32,有的用4個。

interrupt tree 有可能和device tree是不一樣的。
這個node本身如果沒有這interrupt-parent 的話,就是用parent node的interrupt-parent。

interrupts
一個能產生中斷的設備,必須要定義interrups這屬性。這裡指的就是設備產生的interrupt,描述interrupt source的又叫interrupt specifier。使用<u32>來表示。

例:
interrupts = <0xA 8>;
表示interrupt number:0xA, level/sense:8

Properties for Interrupt Controllers
interrupt-controller
Value type:<empty>
用來表示interrupt-controller

interrupt-cells
Value type:<u32>
用來表示interrupt-controller需要幾個單位做interrupt specifier

 Interrupt Nexus Properties
一個interrupt nexus node應該要有一個#interrupt-cells 屬性
interrupt nexus
用來轉譯的,nexus的意思就是"連結",連結上下兩個domains的那個node。
例:
PCI controller device node定義了一個nexus node,用來轉譯PCI interrupt namespace(INTA,INTB,etc.)到interrupt controller的IRQ。

interrupt關係圖示例


上圖中沒有interrupt-parent的open-pic為root,有3個doamin,2個nexus nodes。可轉換為下圖:


2017年7月14日 星期五

Device Tree(2)Properties

Property Names:
由1~31個字元組成,內容為下述:
Character Description 0-9
digit a-z lowercase letter
,(comma ). (period) _ (underscore) +( plus sign) - (dash) ? (question mark) #(hash)

需要一個唯一的prefix,來辨別property的名稱或組織,例如:
fsl,channel-fifo-len
ibm,ppc-interrupt-server#s
linux,network-index

Property Values:
有下面幾種類型,記憶體排方式應該是看CPU 架構為主,little-endian或big-endian:
empty => 如果是true-false的話,可以為空。
u32 => 32bit 整數
u64 => 64bit 整數
string=> 字串
prop-encoded-array=>描述property用
phandle => 一個u32 value,可以用來給別的node拿來參考用,類似指標,指向另一個node。
stringlist => 連續的字串

Standard Properties:
device tree是使用 Device Tree Source(DTS)格式來描述nodes和properties。下面列出一些標準的properties。


Property:compatible
Value type:<stringlist>
用來描述driver model,OS會照stringlist順序去找尋driver model,建議格式為 "manufacturer,model"。
例:
compatible = “fsl,mpc8641-uart”, “ns16550";

在這例子中有兩個string,OS會先去找“fsl,mpc8641-uart”, 如果找不到就會改去找 general ns16650 device type。

Property:model
Value type:<string>
driver名稱及model number。
例:
model = “fsl,MPC8349EMITX”;

Property:phandle
Value type:<u32>
一個在device tree中的唯一值,給其他的node參考用。
例:
pic@10000000 {
    phandle = <1>;
    interrupt-controller; 20
};
定義pandle=1,其他的node可用1這個值來使用pic@10000000。
例:
interrupt-parent = <1>;

Property:status
Value type:<string>
顯示driver的操作,有4種。
okay=>表示可操作
disabled=>表示目前為關閉,不可操作
fail=>表示目前為錯誤,需要修復,不可操作
fail-sss=>表示目前為錯誤,需要修復,不可操作。sss用來指示錯誤原因。

Property:#address-cells and #size-cells
Value type:<u32>
表示在child node's 的reg裡的address and size。如果沒有寫,default為2 and 1。
#size-cells是可以為0的,那reg裡面的size就會被省略。
例:
裡面表示soc的child serial node裡的reg有一個address (0x4600)和一個size(0x100)。

Property:reg
Value type:<prop-encoded-array>, 表示方式為(address, length)
register,主要是指memory-mapped IO register的offsets and lengths。
例:
#address-cells為1 and #size-cells為1
reg = <0x3000 0x20 0xFE00 0x100>;
代表在0x3000有一個32byte的block和0xFE00有一個256 byte block

Property:ranges
Value type:<empty> or <prop-encoded-array>, 表示方式為(child-bus-address, parent-bus-address, length)
表示 register定址的範圍
例:
定址範圍有0x00100000, 1024KB,child 定址在0x0, mapping 到parent 0xe0000000。
在此例中,serial的offset 為04600,而mapping後的位置為0xe0004600

Property:device_type
Value type:<string>
原本在IEEE1275中用來描述FCode,因為ePAPR沒有FCode,為了相容性,只用在cpu and memory上。
例:
 memory {
   device_type = "memory";
   reg = <0x00000000 0x100000000>; /* 4 GB */
 };








Device Tree(1)觀念和介紹

概念:
Linux  Kernel從3.x開始引入設備樹的概念。樹狀結構來描述硬體,每一個node有property/value來描述屬性,除了root node之外,都會有一個parent。原則上是非動態偵測的device,就要加入device tree,例如PCI。

下圖表示一個最簡單可以開機的OS的device tree,包含了平台、CPU、memory等資訊。 



Node Names:
名稱組成方式為 => node-name@unit-address

node-name為長度1~31的大小寫英文數字以及 ,(comma) .(period) _(underscore) +(plus sign) -(dash)組成。開頭必頭為英文字母。

unit-address要符合reg property。unit-address可以省略,只要名稱不要和其他兄弟節點一樣即可。

root node的名稱是slash(/)。

例如ethernet@fe001000和ethernet@fe002000這兩個node就是以unit-address來分別。

建議的node-name:
• atm
• cache-controller
• compact-flash 9
• can
• cpu
• crypto
• disk
• display
• dma-controller
• ethernet
• ethernet-phy
• fdc
• flash
• gpio
• i2c
• ide
• interrupt-controller
• isa
• keyboard
• mdio
• memory
• memory-controller
• mouse
• nvram
• parallel
• pc-card
• pci
• pcie
• rtc
• sata
• scsi
• serial
• sound
• spi
• timer
• usb
• vme
• watchdog



2017年7月6日 星期四

Buildroot (3)常用指令及基本觀念

make list-defconfigs:
列出所有的defconfig,如果這裡有廠商給的config,請放在configs資料夾下,Buildroot才會
偵測到

make help:
使用說明

make clean:
當toolchain變動時或任何大的變動,請下這指令,會刪除所有build產生的東西(including build directories, host, staging and target trees, the images and the toolchain)

make distclean:
刪除所有的檔案,除了source files。注意! 當ccache 開啟時,make clean or make distclean不會刪除cache。

何時要rebuild:
Buildroot不會偵測在make menuconfig, make xconfig後的變動,user要自已判斷需不需要rebuild。關於rebuild判斷的參考:

(1)target architecture configuration變動時,整個rebuild

(2)toolchain configuration變動時,整個rebuild

(3)新增package時,不需要整個rebuild。
a.如果Buildroot 偵測到package沒有built過,則會build it。
b.如果這package是其它package的可選library時,且它已build過,Buildroot不會自動重build這 package。
c.自已手動reubild package或整個Buildroot

Ex:例如我們原本built了一個ctorrent package,裡面不含openssl 。但後來想要支援openssl,我們就在設定裡新增openssl,此時Buildroot只會built新的openssl,不會rebuild
ctorrent! 這時需要我們手動rebuild ctorrent或者整個rebuild。

(4)移除一個package時,Buildroot並不會去刪時它,除非你整個rebuild

(5)package的設定變動時,Buildroot並不會去rebuild這個package

(6)當root filesystem的框架變動時,需要整個rebuild

如何rebuild一個package或移除一個package時不用整個rebuild?移除一個package時,並須要整個rebuild。而最簡單的rebuild一個package的方法是刪除output/build/<package>這個資料夾,可以下 make <package>dirclean來進行移除的動作。

make <package>-rebuild:
重新compilation and installation,這個指令會執行package裡面的makefile。

make <package>-reconfig:
重新configuration, compilation and installation,這個指令會執行package裡面的makefile。

Building out-of-tree:
預設輸出會在output資料夾下,如果想要更改輸出資料夾,請參考下述指令
make O=/tmp/build
or
cd /tmp/build
make O=$PWD -C path/to/buildroot
如果/tmp/build資料夾不存在的話,Buildroot會建立,最終結果會產生在這資料夾下。
這裡可以使用絕對位置和相對位置,相對位置是指相對於 Buildroot source directory。
這裡要注意的是,.config也會移到你改變輸出的位置。

Environment variables:
HOSTCXX =>host C++ Compiler
HOSTCC => host C Compiler
UBLIBC_CONFIG_FILE=<path/to/.config>=>uClibc configuration的位置
BUSYBOX_CONFIG_FILE=<path/to/.config>=>BusyBox configuration的位置
BR2_CCACHE_DIR=>ccache 的位置
BR2_DL_DIR=> 下載下來的檔案位置
BR2_GRAPH_XXX=>產生graph的相關設定

Buildroot 圖片分析指令:
要先確認有安裝graphviz工具,sudo apt-get install graphviz。
make graph-depends:
產生整個Buildroot的depends圖片,位置在output/graphs/graph-depends.pdf 

make <pkg>-graph-depends:
產生單一package的depends圖片,位置在output/graphs/<pkg>-graph-depends.pdf 

make graph-build:
產生整個Buildroot中要build每個package所花費的時間,位置在output/graphs/
  • build.hist-build.pdf, a histogram of the build time for each package, ordered in the build order.
  • build.hist-duration.pdf, a histogram of the build time for each package, ordered by duration (longest first)
  • build.hist-name.pdf, a histogram of the build time for each package, order by package name.
  • build.pie-packages.pdf, a pie chart of the build time per package
  • build.pie-steps.pdf, a pie chart of the global time spent in each step of the packages build process
make graph-size:
產生整個Buildroot中要build每個package所花費的時間,位置在output/graphs/




2017年7月5日 星期三

Buildroot (2)設定

Cross-compilation toolchain:
Buildroot 提供兩種toolchain設定:
1. internal toolchain backend
使用Buildroot自帶的toolchain,支援uClibc-ng, glibc and musl,需選擇Linux kernel headers、C Library、GCC compiler、binutils等工具和版本。

2.external toolchain backend
有三個方式可選:
(1)Use a predefined externel toolchain profile,讓Buildroot去下載安裝
(2)Use a predefined externel toolchain profile,告訴Buildroot這toolchain放在系統那邊,不要去下載安裝
(2)Use a completely externel toolchain,通常這是指晶片商提供的toolchain,我們需要指定Toolchain path,Toolchain prefix and External toolchain C library。一般而言會提供一個設定檔,執行這個檔案,例如 make XXXX_defconfig。再執行make menuconfig,會看到在menu中已經設好了,唯一要手動的是,path和prefix。如果build的時後顯示找不到toolchain,大多是這兩個參數設錯。

例如:
BR2_TOOLCHAIN_EXTERNAL_PATH="/home/<user>/<work directory>/armv7-marvell-linux-gnueabihf/"

BR2_TOOLCHAIN_EXTERNAL_CUSTOM_PREFIX="arm-marvell-linux-gnueabihf"

BusyBox:
可用default的BusyBox設定或使用BR2_PACKAGE_BUSYBOX_CONFIG這個參數來讀取已定義好的設定。也可使用 make busybox-menuconfig 來編輯BusyBox。

uClibc:
和BusyBox類似,使用BR2_UCLIBC_CONFIG來讀取已定義好的設定。也可使用 make uclibc-menuconfig 來編輯BusyBox。

Linux kernel:
使用BR2_LINUX_KERNEL_USE_CUSTOM_CONFIG來讀取已定義好的設定
使用BR2_LINUX_KERNEL_USE_DEFCONFIG來使用default值
使用 make linux-menuconfig 來編輯

Barebox:
U-Boot二代,使用上和Linux kernel類似
使用BR2_LINUX_TARGET_BAREBOX_USE_CUSTOM_CONFIG來讀取已定義好的設定
使用BR2_LINUX_TARGET_BAREBOX_USE_DEFCONFIG來使用default值
使用 make barebox-menuconfig 來編輯

U-Boot:
U-Boot板本要2015.04以上
使用BR2_LINUX_TARGET_UBOOT_USE_CUSTOM_CONFIG來讀取已定義好的設定
使用BR2_LINUX_TARGET_UBOOT_USE_DEFCONFIG來使用default值
使用 make uboot-menuconfig 來編輯

Buildroot (1)介紹

Buildroot是一簡化且自動化處理產生corss-compilation toolchain, root filesystem, Linux Kernel, bootloader的工具,便於開發embedded system。

Host: 主機、宿主,一般而言就是我們的PC、Notebook
Target: 目標設備

安裝Buildroot必須的packages:
  • Build tools:
    • which
    • sed
    • make (version 3.81 or any later)
    • binutils
    • build-essential (only for Debian based systems)
    • gcc (version 2.95 or any later)
    • g++ (version 2.95 or any later)
    • bash
    • patch
    • gzip
    • bzip2
    • perl (version 5.8.7 or any later)
    • tar
    • cpio
    • python (version 2.6 or any later)
    • unzip
    • rsync
    • file (must be in /usr/bin/file)
    • bc
  • Source fetching tools:
    • wget

可選是否要安裝的packages:

    • ncurses5 to use the menuconfig interface
    • qt4 to use the xconfig interface(sudo apt-get install qt4-qmake libqt4-dev)
    • glib2, gtk2 and glade2 to use the gconfig interface
  • Source fetching tools:
    • bazaar
    • cvs
    • git
    • mercurial
    • rsync
    • scp
    • subversion
  • Java-related packages:
    • The javac compiler
    • The jar tool
  • Documentation generation tools:
    • asciidoc, version 8.6.3 or higher
    • w3m
    • python with the argparse module (automatically present in 2.7+ and 3.2+)
    • dblatex (required for the pdf manual only)
  • Graph generation tools:
    • graphviz to use graph-depends and <pkg>-graph-depends
    • python-matplotlib to use graph-build
空間夠的話,建議全部一起安裝,例如git或graphviz(make graph-depends時會用到)又或python都很常用。


Quick Start:
首先我們需要產生一個 .config檔,可使用下面四種指令,用圖形化的視窗來設定:
1. make menuconfig最基本也是最多人用的
2. make nconfig 新的圖形介面
3. make xconfig Qt-based的圖形介面
4. make gconfig GTK-based的圖形介面

只是畫面不同,重點在設定完,會在目前目錄下(也就是top-level的目錄)產生一個.config檔(隱藏檔),這裡面有你設定好的設定,之後也可直接更改此檔來做設定。設定完後下make,沒意外的話,一切就會自動完成。

要注意的是,這裡不能在top-level資料夾下 make -jN的指令,一般我們會在4核下的cpu 裡下make -j8來進行多工的處理,可是Buildroot不支援在top-level的並行處理。取而代之的是使用BR2_JLEVEL。這選項告訴Buildroot在每個獨立的package裡下make -jN,例如修改.config的BR2_JLEVEL=8。

make的流程:
  • download source files (as required);
  • configure, build and install the cross-compilation toolchain, or simply import an external toolchain;
  • configure, build and install selected target packages;
  • build a kernel image, if selected;
  • build a bootloader image, if selected;
  • create a root filesystem in selected formats.
最後結果輸出在output資料夾。

output資料夾內容:
images => kernel image,bootloader, root filesystem images等等
build => 所有下載或自定義build好的packages
staging => 這資料包含一些研發工具,用來compile libraries and applications給target使用
target => 目標設備的root filesystem範本,依照手冊說明,不包含dev,因為make的時後並     不是用root啟動,這裡比較奇怪,我沒有下sudo make,可是target底下也是有dev這資料夾?   也有可能是之前有下過sudo make,而資料夾未被移除?
host => 主機上bulidroot所需的工具
graphs=>如果下make graph-depends等輔助指令,可產生package的相依性、生成的size、      build的時間等等資訊,會放在這個資料夾裡

2017年6月22日 星期四

U-Boot make error=>dirname: missing operand

在compile Marvell的U-Boot時,出現dirname: missing operand。
一開始以為是makefile傳入的參數錯誤,但不是。
後來上網查,說是有兩個原因。

1:
在64bit OS下運行32 bit的compile,建議裝"ia32-libs",又有人說"ia32-libs"太舊,
要改裝"libc6:i386"。我確實是在64bit 的Ubuntu 16下運行,但都裝了也是不行。

2:
toolchain的位置錯誤。這個讓我try了好久,一般之前我都是設定toolchain位置的bin資料夾即可,但是U-Boot上要加上compiler的prefix才行。

原本
export CROSS_COMPILE=/<your toolchain path>/armv7-marvell-linux-gnueabi/bin
改成
export CROSS_COMPILE=/<your toolchain path>/armv7-marvell-linux-gnueabi/bin/arm-marvell-linux-gnueabi-

這樣make時會合併prefix字串變成arm-marvell-linux-gnueabi-gcc、arm-marvell-linux-gnueabi-ld等等。實際上的prefix要看你的bin底下的compile tool name為準。

Bitbake(2)Hello World Example

從Hello World來理解bitbake的處理過程:


1.安裝Bitbake並設定PATH
使用git下載 bitbake並安裝
$git clone git://git.openembedded.org/bitbake
設定PATH
$export PATH=/home/<your directory>/bitbake/bin:$PATH

2. 新建一個工作目錄,並試著run bitbake
$mkdir ~/hello
$cd hello
$bitbake
會出現下面的錯誤訊息
The BBPATH variable is not set and bitbake did not
     find a conf/bblayers.conf file in the expected location.
     Maybe you accidentally invoked bitbake from the wrong directory?
     DEBUG: Removed the following variables from the environment:
     GNOME_DESKTOP_SESSION_ID, XDG_CURRENT_DESKTOP,
     GNOME_KEYRING_CONTROL, DISPLAY, SSH_AGENT_PID, LANG, no_proxy,
     XDG_SESSION_PATH, XAUTHORITY, SESSION_MANAGER, SHLVL,
     MANDATORY_PATH, COMPIZ_CONFIG_PROFILE, WINDOWID, EDITOR,
     GPG_AGENT_INFO, SSH_AUTH_SOCK, GDMSESSION, GNOME_KEYRING_PID,
     XDG_SEAT_PATH, XDG_CONFIG_DIRS, LESSOPEN, DBUS_SESSION_BUS_ADDRESS,
     _, XDG_SESSION_COOKIE, DESKTOP_SESSION, LESSCLOSE, DEFAULTS_PATH,
     UBUNTU_MENUPROXY, OLDPWD, XDG_DATA_DIRS, COLORTERM, LS_COLORS


當我們啟動 bitbake時,它會試著去找medtata files。而BBPATH就是指示那些檔案放那的變數。
如果沒有設定它,bitbake不知去那找.conf和.bb。

2.設定BBPATH,run bitbake

$BBPATH="/home/<your directory>/hello"
$export BBPATH
$bitbake

ERROR: Traceback (most recent call last):
       File "/home/scott-lenovo/bitbake/lib/bb/cookerdata.py", line 163, in wrapped
         return func(fn, *args)
       File "/home/scott-lenovo/bitbake/lib/bb/cookerdata.py", line 173, in parse_config_file
         return bb.parse.handle(fn, data, include)
       File "/home/scott-lenovo/bitbake/lib/bb/parse/__init__.py", line 99, in handle
         return h['handle'](fn, data, include)
       File "/home/scott-lenovo/bitbake/lib/bb/parse/parse_py/ConfHandler.py", line 120, in handle
         abs_fn = resolve_file(fn, data)
       File "/home/scott-lenovo/bitbake/lib/bb/parse/__init__.py", line 117, in resolve_file
         raise IOError("file %s not found in %s" % (fn, bbpath))
     IOError: file conf/bitbake.conf not found in /home/scott-lenovo/hello

     ERROR: Unable to parse conf/bitbake.conf: file conf/bitbake.conf not found in /home/scott-lenovo/hello


找不到conf/bitbake.conf,而這個檔案應該建在hello底下。

3.建立conf/bitbake.conf,run bitbake

先在hello底下建立conf資料夾
$ mkdir conf
再在conf下建立bitbake.conf,並輸入底下內容

TMPDIR  = "${TOPDIR}/tmp"
CACHE   = "${TMPDIR}/cache"
STAMP   = "${TMPDIR}/stamps"
T       = "${TMPDIR}/work"
B       = "${TMPDIR}"

TOPDIR => build directory,也就是hello資料夾
TMPDIR => 建立在build資料夾下
CACHE => 建立在TMPDIR(tmp)資料夾下,儲存Meatadata的cache,讓Bitbake不用每次重新parse
STAMP => 用來建立recipe stamp files
T => 用來放暫存檔的地方,大多是task logs and scriptsS。
B => Bitbake建置recipes時,執行functions的地方

執行Bitbake
$bitbake

ERROR: Traceback (most recent call last):
  File "/home/scott-lenovo/bitbake/lib/bb/cookerdata.py", line 163, in wrapped
    return func(fn, *args)
  File "/home/scott-lenovo/bitbake/lib/bb/cookerdata.py", line 177, in _inherit
    bb.parse.BBHandler.inherit(bbclass, "configuration INHERITs", 0, data)
  File "/home/scott-lenovo/bitbake/lib/bb/parse/parse_py/BBHandler.py", line 92, in inherit
    include(fn, file, lineno, d, "inherit")
  File "/home/scott-lenovo/bitbake/lib/bb/parse/parse_py/ConfHandler.py", line 100, in include
    raise ParseError("Could not %(error_out)s file %(fn)s" % vars(), oldfn, lineno)
ParseError: ParseError in configuration INHERITs: Could not inherit file classes/base.bbclass

ERROR: Unable to parse base: ParseError in configuration INHERITs: Could not inherit file classes/base.bbclass

這次是找不到classes/base.bbclass。classes是用來放基本的classes,它類似物件導向語言裡的classes。
base.bbclass是用來給每個recipe繼承的。

4.建立classes/base.bbclass,run bitbake

建立classes資料夾
$ cd $HOME/hello
$ mkdir classes

在base.bbclass輸入

addtask build

執行bitbake
 $ bitbake
     Nothing to do.  Use 'bitbake world' to build everything, or run 'bitbake --help' for usage information.

5. 新建一個mylayer和printhello.bb,run bitbake printhello.bb
 $ cd $HOME
 $ mkdir mylayer
 $ cd mylayer
 $ mkdir conf

在conf資料夾下新增一個layer.conf並輸入下面內容:
 BBPATH .= ":${LAYERDIR}"
 BBFILES += "${LAYERDIR}/*.bb"
 BBFILE_COLLECTIONS += "mylayer"
 BBFILE_PATTERN_mylayer := "^${LAYERDIR}/"

在mylayer資料夾下新增一個printhello.bb並輸入下面內容:
DESCRIPTION = "Prints Hello World"
     PN = 'printhello'
     PV = '1'

     python do_build() {
        bb.plain("********************");
        bb.plain("*                  *");
        bb.plain("*  Hello, World!   *");
        bb.plain("*                  *");
        bb.plain("********************");
     }

執行 bitbake printhello
$ cd $HOME/hello
$ bitbake printhello
ERROR: no recipe files to build, check your BBPATH and BBFILES?

Summary: There was 1 ERROR message shown, returning a non-zero exit code.
因為bitbake不知printhello.bb在那裡。

6.新增bblayers.conf,執行bitbake printhello
在hello/conf底下,新增bblayers.conf告訴bitbake printhello.bb的位置
BBLAYERS ?= " \
       /home/<you>/mylayer \

執行 bitbake printhello
 $ bitbake printhello
     Parsing recipes: 100% |##################################################################################|
     Time: 00:00:00
     Parsing of 1 .bb files complete (0 cached, 1 parsed). 1 targets, 0 skipped, 0 masked, 0 errors.
     NOTE: Resolving any missing task queue dependencies
     NOTE: Preparing runqueue
     NOTE: Executing RunQueue Tasks
     ********************
     *                  *
     *  Hello, World!   *
     *                  *
     ********************
     NOTE: Tasks Summary: Attempted 1 tasks of which 0 didn't need to be rerun and all succeeded.

2017年6月15日 星期四

BitBake (1)Overview

BitBake是OpenEmbedded的一個 task executor,使用Python撰寫,主要功用是分析recipes後建置輸出package,也被Yocto Project採用。觀念上,BitBake類似GNU Make。而makefile也就是recipe。它分析recipe後,可得知建置所需的dependencies, source file locations, checksums, description....等等資訊,再開始建置。下面是Bitbake的幾種data 類型:


Recipes:
描述source code 的位置、和如何configure and compile the source code等等資訊。副檔名為.bb。

Configuration Files:
定義machine configuration options, distribution configuration options, compiler tuning options,general common configuration options, and user configuration options。副檔名為.conf。


Classes:
抽象的基本build subroutines,使用inheritance mechanism(繼承機制)提供創出新的.bb檔,類似物件導象中的Class。副檔名為.bbclass。

Layers:
Metadata 是由許多的layers組成。這些layers允許你分離不同客制化的設定。

Append Files:
一般.bb和.bbappend會使用相同的檔名,用副檔名來區分。副檔名為.bbappend。

2016年11月27日 星期日

c++ - non-trivial designated initializers not supported

很久沒用C++了,從C又換到C++編譯器遇到的一個小問題,但是也困擾了很久...
在C的complier上,struct或enum,可以指定初值且不按順序,但在C++上指定初值時
並須按順序且不能跳過。

這是因為C++指援不定引數的功能,例如在function上傳的引數,可以在runtime時
決定要傳幾個。例如void fun(int a, int b=1, int c=2),而在使用時可決定只用fun(10)或fun(10,11,12)。 但是不能跳過,例如void fun(int a=2, int b, int c=3),只能從最右邊開始
給定初值 。

這次程式中在定義enum並指定初值的時後,有一個值未defined造成有一個值跳過去,故
一直出現此錯誤,但這個寫法在C的complier上是很常見的。

補充:
non-trivial constructor/destructor
意思是指class或struct的非默認建構/解構子

2016年11月1日 星期二

Ubuntu - change shell from dash to bash

因為Ubuntu 16的預設shell 是dash,和之前Fedora的bash不同,在語法上也有差異。
dash是bash的輕量版,執行速度較快,Ubuntu建議使用dash來獲得
更好的效能。但是之前寫的script也懶的改了,還是更改預設的shell比較快。
更改方法:
1.在執行前加bash, bash ./your_shell_script,但每次都要加
2.sudo dpkg-reconfigure dash,選no,default就會跳會bash,此法一勞永逸


2016年10月27日 星期四

linux指令 - 列出指定目錄下的所有目錄路徑

為了要使用kdevelop來看code,要手動加入所有include路徑,只好改用指令過瀘出路徑,再存成一個檔案,最後用batch edit來加入。

下面這指令是列出指定目錄下的所有目錄路徑,並忽略隱藏檔,還有要忽略build這個資料夾(包含其子目綠),最後存到include_dirs
find /home/hughes/CEServices-4.00.01/src -type d ! -name ".*" | grep -v build > /home/hughes/include_dirs

簡單的拆開來看:
1. 找出src底下的目錄並忽略隱藏檔
2. 結果重導給grep,並找尋路徑中不含"build"的內容
3.存到include_dirs

2016年10月26日 星期三

kubuntu16 安裝問題

使用vmware安裝 kubuntu16 時,出現Unable to find a medium containing a live file system。
上網查了一下,有可能是檔案下載不完全或者光碟機老舊或光碟機不支援。

重新下載了一次,也是一樣結果。後來把vmware的光碟機由sata 變為ide就可以了。

2016年10月24日 星期一

CMake筆記(6) - 編譯選項

Makefile的建置資訊:
在預設的情況下,CMake 生成的 makefile 只會顯示編譯的進度,並不會把各步驟實際調用的命令、參數一一列出。如果需要這類資訊的話,就可設定makefile中的verbose,有3種方法:

1.在CMakeList.txt 中加入, set(CMAKE_VERBOSE_MAKEFILE ON)
2.在cmake執行時加入,$ cmake -DCMAKE_VERBOSE_MAKEFILE=ON ...options...
3.在make執行時加入,make VERBOSE=1

編譯組態:
透過指定 CMAKE_BUILD_TYPE 變數即可改變編譯組態,預設的 CMAKE_BUILD_TYPE 是 None,即不加上任何額外編譯選項。內建的 CMAKE_BUILD_TYPE 支援以下幾種組態:
  • None : 編譯器預設值
  • Debug : 產生除錯資訊
  • Release : 進行執行速度最佳化
  • RelWithDebInfo : 進行執行速度最佳化,但仍然會啟用 debug flag
  • MinSizeRel : 進行程式碼最小化

也可以從cmake執行時才加入,例如:
cmake -DCMAKE_BUILD_TYPE=Release ./src

編譯選項:
變數 CMAKE_C_FLAGS 存放的內容會被傳給 C 編譯器,作用在所有的編譯組態上。如果希望只針對特定一種組態有效,可以設定 CMAKE_C_FLAGS_<編譯組態>,例如 CMAKE_C_FLAGS_RELEASE、CMAKE_C_FLAGS_DEBUG。如果是C++編譯器,則是CMAKE_CXX_FLAGS 和 CMAKE_CXX_FLAGS_<編譯組態>。
舉個具體的例子,在 Visual C++ 底下編譯含有 sprintf 的原始碼時,常常收到 C4996 警告,除了可以在原始碼中使用 #pragma 關閉,也可以透過編譯選項搞定。在 CMakeLists.txt 設定編譯選項的方式如下:

if(MSVC)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /wd4996")
endif()

連結選項:
CMAKE_EXE_LINKER_FLAGS 變數決定了連結執行檔時傳給編譯器的選項,同樣也有各種不同編譯組態的變形。下例為加入靜態連結:

if(MINGW)
    set(CMAKE_EXE_LINKER_FLAGS "-static")
    set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-s")
endif()

上例的各種組態都採用 -static 來連結可執行檔,並且只有在 Release 情況下另外加入 -s 連結選項,亦即 Release 的連結選項為 -static -s

Preprocessor:
add_definitions("-DFOO -DBAR")

編譯器搜尋路徑:
以下兩個指令分別用來加入編譯器尋找標頭檔、程式庫的路徑,在 gcc 相當於 -I 和 -L 選項。
include_directories([AFTER|BEFORE] [SYSTEM] dir1 dir2 ...) 
link_directories(dir1 dir2 ...)

使用屬性控制編譯選項:
以上介紹的變數和指令都是屬於全域範圍的設定,如果希望選項僅對一個 target 有效,就必須從 target 的屬性(properties)下手。

在 CMake 裡一個 target 最終的輸出檔名、編譯連結選項等等都是由屬性控制,若沒有手動設定這些屬性,則 CMake 會由相關的全域變數來決定屬性值。我們可以利用 set_target_properties() 指令單獨指定一個 target 的屬性,如此一來就可以蓋過全域變數的設定。

set_target_properties() 指令格式如下:
set_target_properties(target1 target2 ...
    PROPERTIES
    屬性名稱1  值
    屬性名稱2  值
    ...
    )

例如下例,這些屬性會蓋過原先全域變數的設定,而且僅作用在 app 這個 target 上:
set_target_properties(app
    PROPERTIES
    LINK_FLAGS          -static
    LINK_FLAGS_RELEASE  -s
    )

注意事項:
1. 小心覆寫變數
像 CMAKE_C_FLAGS_RELEASE、CMAKE_C_FLAGS_DEBUG 這些變數,CMake 會自動依照 Generator 指定的編譯器初始化。例如在 gcc 下 CMAKE_C_FLAGS_RELEASE 預設的值為 -O3 -DNDEBUG,而 CMAKE_C_FLAGS_DEBUG 預設值為 -g。

小心下面這種寫法:
set(CMAKE_C_FLAGS_RELEASE "-fno-inline")

原意可能是希望在做最佳化時忽略 inline 關鍵字,但是這樣一來其實會把原本的 -O3 -DNDEBUG 給覆寫掉,所以程式並不會做任何最佳化。

比較好的寫法應該是:
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -fno-inline")

2.注意字串和串列的不同

set(CMAKE_EXE_LINKER_FLAGS "-static")
# ... (1)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -s")
# ... (2)   
set(CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS} "-s")   

(1) 會傳遞 -static -s 給編譯器 (2) 會傳遞 -static;-s 給編譯器

實際應用的範例:
設定g++為編譯器,並build release的版本:

cmake_minimum_required (VERSION 2.6)

project (Test)

set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_CXX_FLAGS "-Wall")
set(CMAKE_CXX_FLAGS_DEBUG "-g3")
set(CMAKE_CXX_FLAGS_RELEASE "-O2")

if(${CMAKE_BUILD_TYPE} MATCHES "debug")
    message("debug version")
else()
    message("release version")
endif()

include_directories(lib/include)
set(TEST_SOURCES src/main.cxx)
set(LIB_SOURCES lib/lib.cxx)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/build)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
 

add_library(MyLib ${LIB_SOURCES})
add_executable(TestRun ${TEST_SOURCES})
target_link_libraries(TestRun MyLib)