
# CSKY工具链在YOCTO环境移植


## 1.  工具链安装
### 1.1 下载工具链 + sysroot 软件：
```bash
csky-linux-gnuabiv2-tools-x86_64-glibc-linux-4.9.56-20210127.tar.gz
```

### 1.2  安装工具链
```bash
mkdir ~/.thead
```

1). 将下载的 csky-linux-gnuabiv2-tools-x86_64-glibc-linux-4.9.56-20210127.tar.gz 解压到  ~/.thead
```bash
tar zxf csky-linux-gnuabiv2-tools-x86_64-glibc-linux-4.9.56-20210127.tar.gz -C ~/.thead
ls ~/.thead
   csky-linux-4.9.56
 ln -s   ~/.thead/csky-linux-4.9.56    ~/.thead/csky-linux
```

2). 工具链名重链接
```bash
ln -s csky-linux-gnuabiv2-addr2line      csky-linux-addr2line
ln -s csky-linux-gnuabiv2-ar             csky-linux-ar
ln -s csky-linux-gnuabiv2-as             csky-linux-as
ln -s csky-linux-gnuabiv2-c++            csky-linux-c++
ln -s csky-linux-gnuabiv2-c++filt        csky-linux-c++filt
ln -s csky-linux-gnuabiv2-cpp            csky-linux-cpp
ln -s csky-linux-gnuabiv2-elfedit        csky-linux-elfedit
ln -s csky-linux-gnuabiv2-g++            csky-linux-g++
ln -s csky-linux-gnuabiv2-gcc            csky-linux-gcc
ln -s csky-linux-gnuabiv2-gcc-6.3.0      csky-linux-gcc-6.3.0
ln -s csky-linux-gnuabiv2-gcc-ar         csky-linux-gcc-ar
ln -s csky-linux-gnuabiv2-gcc-nm         csky-linux-gcc-nm
ln -s csky-linux-gnuabiv2-gcc-ranlib     csky-linux-gcc-ranlib
ln -s csky-linux-gnuabiv2-gcov           csky-linux-gcov
ln -s csky-linux-gnuabiv2-gcov-tool      csky-linux-gcov-tool
ln -s csky-linux-gnuabiv2-gdb            csky-linux-gdb
ln -s csky-linux-gnuabiv2-gfortran       csky-linux-gfortran
ln -s csky-linux-gnuabiv2-gprof          csky-linux-gprof
ln -s csky-linux-gnuabiv2-ld             csky-linux-ld
ln -s csky-linux-gnuabiv2-ld.bfd         csky-linux-ld.bfd
ln -s csky-linux-gnuabiv2-nm             csky-linux-nm
ln -s csky-linux-gnuabiv2-objcopy        csky-linux-objcopy
ln -s csky-linux-gnuabiv2-objdump        csky-linux-objdump
ln -s csky-linux-gnuabiv2-ranlib         csky-linux-ranlib
ln -s csky-linux-gnuabiv2-readelf        csky-linux-readelf
ln -s csky-linux-gnuabiv2-size           csky-linux-size
ln -s csky-linux-gnuabiv2-strings        csky-linux-strings
ln -s csky-linux-gnuabiv2-strip          csky-linux-strip
```
## 2 创建meta-csky

1). 参考meta-riscv的目录结构创建目录
```bash
cp meta-riscv meta-csky
```
 
### 2.1  配置修改

修改文件conf/machine/include/thead-base.inc
```bash
require conf/machine/include/csky/tune-csky.inc

EXTERNAL_TOOLCHAIN ?= "${HOME}/.thead/csky-linux"
TCMODE="external"
TARGET_PREFIX="csky-linux-gnuabiv2-"
TARGET_ARCH="csky"
TARGET_OS="linux"
```
详细参见解压后的meta-csky.tgz
tar xzvf meta-csky.tgz .

 
## 3. openembedded-core中追加对CSKY架构的支持
### 3.1  修改的范围
```bash
modified:   meta/classes/kernel-arch.bbclass
modified:   meta/classes/package.bbclass
modified:   meta/classes/siteinfo.bbclass
modified:   meta/lib/oe/elf.py
modified:   meta/recipes-bsp/opensbi/opensbi_0.8.bb
modified:   meta/recipes-connectivity/openssl/openssl_1.1.1h.bb
```

### 3.2  修改点如下
```bash
diff --git a/meta/classes/kernel-arch.bbclass b/meta/classes/kernel-arch.bbclass
index 07ec242e63..2d6ea39d23 100644
--- a/meta/classes/kernel-arch.bbclass
+++ b/meta/classes/kernel-arch.bbclass
@@ -31,6 +31,7 @@ def map_kernel_arch(a, d):
     elif re.match('mips(isa|)(32|64|)(r6|)(el|)$', a):      return 'mips'
     elif re.match('mcf', a):                    return 'm68k'
     elif re.match('riscv(32|64|)(eb|)$', a):    return 'riscv'
+    elif re.match('csky', a):    return 'csky'diff --git a/meta/classes/package.bbclass b/meta/classes/package.bbclass
index e6236c0bb2..191026d344 100644
--- a/meta/classes/package.bbclass
+++ b/meta/classes/package.bbclass
@@ -1429,7 +1429,8 @@ python populate_packages () {
             mkdir_recurse(dvar, root, os.path.dirname(file))
             fpath = os.path.join(root,file)
             if not cpath.islink(file):
-                os.link(file, fpath)
+                if not os.path.exists(fpath):
+                    os.link(file, fpath)
                 continue
             ret = bb.utils.copyfile(file, fpath)
             if ret is False or ret == 0:
diff --git a/meta/classes/siteinfo.bbclass b/meta/classes/siteinfo.bbclass
index 0bd1f36805..7cd249d94c 100644
--- a/meta/classes/siteinfo.bbclass
+++ b/meta/classes/siteinfo.bbclass
@@ -54,6 +54,7 @@ def siteinfo_data_for_machine(arch, os, d):
         "ppc64le" : "endian-little bit-64 powerpc-common",
         "riscv32": "endian-little bit-32 riscv-common",
         "riscv64": "endian-little bit-64 riscv-common",
+        "csky": "endian-little bit-32 csky-common",
         "sh3": "endian-little bit-32 sh-common",
         "sh3eb": "endian-big bit-32 sh-common",
         "sh4": "endian-little bit-32 sh-common",
@@ -118,6 +119,8 @@ def siteinfo_data_for_machine(arch, os, d):
         "riscv64-linux": "riscv64-linux",
         "riscv64-linux-musl": "riscv64-linux",
         "x86_64-cygwin": "bit-64",
+        "csky-linux": "csky-linux",
+        "csky-linux-musl": "csky-linux",
         "x86_64-darwin": "bit-64",
         "x86_64-darwin9": "bit-64",
         "x86_64-linux": "bit-64",
diff --git a/meta/lib/oe/elf.py b/meta/lib/oe/elf.py
index df0a4593fa..a861b6f1fe 100644
--- a/meta/lib/oe/elf.py
+++ b/meta/lib/oe/elf.py
@@ -24,6 +24,7 @@ def machine_dict(d):
                         "microblaze":  (189,   0,    0,          False,         32),
                         "microblazeel":(189,   0,    0,          True,          32),
                         "powerpc":    (20,     0,    0,          False,         32),
+                        "csky":       (252,    0,    0,          True,          32),
                         "riscv32":    (243,    0,    0,          True,          32),
                         "riscv64":    (243,    0,    0,          True,          64),
                       },
@@ -53,6 +54,7 @@ def machine_dict(d):
                         "mipsisa64r6":   ( 8,  0,    0,          False,         64),
                         "mipsisa64r6el": ( 8,  0,    0,          True,          64),
                         "nios2":      (113,    0,    0,          True,          32),
+                        "csky":       (252,    0,    0,          True,          32),
                         "riscv32":    (243,    0,    0,          True,          32),
                          "riscv64":    (243,    0,    0,          True,          64),
                         "s390":       (22,     0,    0,          False,         32),
@@ -80,6 +82,7 @@ def machine_dict(d):
                         "mips64el":   (   8,     0,    0,          True,          64),
                         "microblaze":  (189,     0,    0,          False,         32),
                        "microblazeel":(189,     0,    0,          True,          32),
+                        "csky":    (252,      0,    0,          True,          32),
                         "riscv32":    (243,      0,    0,          True,          32),
                         "riscv64":    (243,      0,    0,          True,          64),
                         "sh4":        (  42,     0,    0,          True,          32),



```
### 3.2  详细参见patch
```bash
open-core.diff
```


### 3.3  增加CSKY工具链配置
```bash
meta/conf/machine/include/csky

csky/
├── arch-csky.inc
├── qemuriscv.inc
└── tune-csky.inc
```

### 3.4 解压 csky.tgz
```bash
cd riscv_yocto/openembedded-core
tar xzvf  csky.tgz .
```


## 4. 编译
```bash
# 在 yocto 工程根目录 riscv_yocto/ 下执行：
sh openembedded-core/envsetup.sh -a csky -i thead-build/ice-base  # 将 build 目录指定为 thead-build/ice-base
# 确认 downloads 文件夹：设置一个存放 yocto 下载文件的共享目录 SHARE_YOCTO_DOWNLOAD_FILE_FOLDER
ln -s SHARE_YOCTO_DOWNLOAD_FILE_FOLDER/ ../downloads

# 编译：thead-build/ice-base/conf/auto.conf 中默认设置为 MACHINE ?= "ice"
bitbake core-image-minimal

# sstate-cache/ 会放在 thead-build/ 下，不同配置可以共享使用
```
### 4.1 输出
```bash
# 镜像文件
./build/tmp-glibc/deploy/images/ice
```

## 5. CSKY与RISCV编译修改说明
### 5.1  envsetup.sh脚本说明-(新增脚本)
```bash
Usage: pack [-cARCH] [-oIMAGE] [-h]
        -a ARCH (default: riscv)
        -o IMAGE_DIR (default: thead-build/ice-base)
        -h print this help message
```

### 5.2  架构如何匹配
```bash
 架构匹配的统一名称：meta-arch
 rm -rf $T/meta-arch
 ln -s $T/meta-$ARCH  $T/meta-arch
```

###5.3 修改bblayers.conf(以ice-base为例)
```bash
/riscv_yocto/thead-build/ice-base/conf$ git diff bblayers.conf
diff --git a/ice-base/conf/bblayers.conf b/ice-base/conf/bblayers.conf
index d6f6ef7..3600ec7 100644
--- a/ice-base/conf/bblayers.conf
+++ b/ice-base/conf/bblayers.conf
@@ -15,5 +15,5 @@ BBLAYERS ?= " \
   ${YOCTOROOT}/meta-openembedded/meta-multimedia \
   ${YOCTOROOT}/meta-openembedded/meta-networking \
   ${YOCTOROOT}/meta-external-toolchain \
-  ${YOCTOROOT}/meta-riscv \
+  ${YOCTOROOT}/meta-arch \
   "
```
###5.4 如何编译
1. source openembedded-core/envsetup.sh
```bash
   Setup env done! Please run config next.
```
2. config(可以直接config 1或2)
```bash
/riscv_yocto/thead-build/ice-base# config

You're building on Linux

Config menu... pick a combo:
     1. riscv
     2. csky

Which would you like? [Default riscv]: 
```

3. 查看帮助
```bash
/riscv_yocto/thead-build/ice-base# help
Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:

== before all ==
- config:        config <arch_name>

== build project ==
- mbase:       Build ice_base.
- mxfce:       Build ice_xfce.
- mgnome:      Build ice_gnome.
- msdk:        Build SDK.

== jump directory ==
- ctarget:   Jump to target directory.
- cdeb:    Jump to target deb directory.
- cimage:  Jump to target image directory.
```

备注： ice-gnome 和 ice-xfce下的bblayers.conf文件同样修改