Porting Source Code
Prerequisites
- The Kunpeng DevKit has been installed.
- Git has been installed and the environment variables have been configured.
Procedure
- Prepare the Megahit source code.
- Use MobaXterm to log in to the server as the root user.
- If the source code porting directory exists, skip this step. Otherwise, manually create a source code porting directory. The following uses devadmin as an example.
1 2 3
mkdir -p /opt/DevKit/workspace/devadmin/porting/sourcecode/ chmod -R 700 /opt/DevKit/workspace/devadmin/porting/ chown -R username:username /opt/DevKit/workspace/devadmin/porting/
- /opt indicates the DevKit installation path. Replace it with the actual one.
- username indicates the DevKit installation user. Replace it with the actual user.
- Obtain the Megahit source code from GitHub, upload it to the directory, and extract it.
1 2
tar -zxvf megahit-1.2.9.tar.gz cd megahit-1.2.9
- Create a build folder and go to the folder.
1mkdir build && cd build
- Generate Makefile.
1cmake .. -DCMAKE_BUILD_TYPE=Release
- Download the updated Megahit source code to the local PC and run the following command to delete the Megahit source code in the source code porting directory:
1rm -rf /opt/DevKit/workspace/devadmin/porting/sourcecode/megahit-1.2.9
- Log in to the Kunpeng DevKit. In the navigation tree on the left, choose . Then click
to go to the Source Code Porting tab page. - Upload the Megahit source code folder. Select BiSheng Compiler 2.5.0.1 for Compiler Version and make -C build for Compile Command. Retain the default values for the other parameters. Then click Start.
- VS Code
Figure 1 Creating a task
- WebUI
Figure 2 Creating a task
- VS Code
- After the analysis is complete, click Porting Report and view the modification suggestions.Figure 3 Viewing the report
- Under Source Files to Be Ported, select a source file and click View Suggested Source Code in the Operation column.Figure 4 Viewing files to be ported
This project is compiled using CMake and the CMake middleware is used to demonstrate the Makefile scan function. As a result, some false scan information may appear. You do not need to modify these suggestions in this best practice. For example, suggestions provided in the WebUI are as follows:

- The phmap.h, phmap_bits.h, phmap_config.h, and xxh3.h source files already have macro definitions. The header files are defined only when SSE2 and SSSE3 exist in the source code structure. The macro definitions have two nested layers and do not need to be modified.
- Except the flags.make file that needs to be modified in the following steps, other .make files do not need to be modified.
- Based on the suggestions, modify the code. Click the embedded assembly code cpu_dispatch.h and you will see that five points need to be modified while one does not.
- VS Code
Figure 5 Viewing the code to be modified
- WebUI
Figure 6 Viewing the code to be modified
- Apply auto-modification to points 1 and 4:
- VS Code
Figure 7 Applying auto-modification (point 1)
After auto-modification:
Figure 8 After auto-modification (point 1)
- WebUI
Figure 9 Applying auto-modification (point 1)
After auto-modification:
Figure 10 After auto-modification (point 1)
- VS Code
- Modification points 3 and 5: Click Quick Fix > Batch Apply This Suggestion to apply the automatic modification.
- VS Code
Figure 11 Applying auto-modification (point 2)
After auto-modification:
Figure 12 After auto-modification (point 2)
Figure 13 After auto-modification (point 3)
You can see that specific suggestions have been provided after automatic modification is applied. The suggestions provided for the two points are correct.
Figure 14 Modification result (point 1)
Figure 15 Modification result (point 2)
- WebUI
Figure 16 Applying auto-modification (point 2)
After auto-modification:
Figure 17 After auto-modification (point 2)
Figure 18 After auto-modification (point 3)
You can see that specific suggestions have been provided after automatic modification is applied. The suggestions provided for the two points are correct.
Figure 19 Modification result (point 1)
Figure 20 Modification result (point 2)
- VS Code
- Save the modification.
- VS Code: After modifying the file, press Ctrl+S to save the modification.
- WebUI: After modifying the file, click Save.
- VS Code
- Move the cursor to the three highlighted code snippets of the kmrns.h file. One of the code snippets needs to be modified and the other two do not.
- VS Code
Figure 21 Code snippet to be modified
- WebUI
Figure 22 Code snippet to be modified
Megahit has been partially adapted to Arm. Therefore, you only need to use the automatic modification function and do not need to use the avx2ki.h header file.
- Apply auto-modification and the code is as follows:
- VS Code
Figure 23 After modification
- WebUI
Figure 24 After modification
- VS Code
- Save the modification.
- VS Code: After modifying the file, press Ctrl+S to save the modification.
- WebUI: After modifying the file, click Save.
- VS Code
- Move the cursor to the megahit_core.dir/flags.make file. You can see two modification points, but only one needs to be modified.
- VS Code
Figure 25 Code snippet to be modified
- WebUI
Figure 26 Code snippet to be modified
- According to the suggestions (the Kunpeng platform does not support BMI2 or POPCNT instructions), manually modify the code. Delete the -mbmi2 and -mpopcnt instructions.
- VS Code
Figure 27 Manual modification
After modification:
Figure 28 After manual modification
- WebUI
Figure 29 Manual modification
After modification:
Figure 30 After manual modification
- VS Code
- Save the modification.
- VS Code: After modifying the file, press Ctrl+S to save the modification.
- WebUI: After modifying the file, click Save.
- VS Code
- Move the cursor to the megahit_core_popcnt.dir/flags.make file. You can see two modification points, but only one needs to be modified.
- VS Code
Figure 31 Code snippet to be modified
- WebUI
Figure 32 Code snippet to be modified
- Apply auto-modification and the code is as follows:
- VS Code
Figure 33 After modification
- WebUI
Figure 34 After modification
- VS Code
- Save the modification.
- VS Code: After modifying the file, press Ctrl+S to save the modification.
- WebUI: After modifying the file, click Save.
- VS Code
- Recompile the code after the migration.
- Add the KunpengTrans.h file to the /opt/DevKit/workspace/devadmin/porting/sourcecode/megahit-1.2.9/src/utils directory.
Run the following copy command:
1cp /opt/DevKit/devkitplugins/inline_asm/config/KunpengTrans.h /opt/DevKit/workspace/devadmin/porting/sourcecode/megahit-1.2.9/src/utils
- Run the make command:
1cd /opt/DevKit/workspace/devadmin/porting/sourcecode/megahit-1.2.9/build && make
- Add the KunpengTrans.h file to the /opt/DevKit/workspace/devadmin/porting/sourcecode/megahit-1.2.9/src/utils directory.