Skip to content

Instantly share code, notes, and snippets.

@yuanliwei
Last active December 27, 2015 15:44
Show Gist options
  • Save yuanliwei/5231d3a5450e78c00b2a to your computer and use it in GitHub Desktop.
Save yuanliwei/5231d3a5450e78c00b2a to your computer and use it in GitHub Desktop.
使用PowerShell编译C51项目

使用PowerShell编译C51项目

$BIN_PATH = "E:\Tools\Keil\C51"
$env:Path = $env:Path + ";$BIN_PATH\BIN"
$env:TMP
$env:C51INC = "$BIN_PATH\INC"
$env:C51LIB = "$BIN_PATH\LIB"

$partternErr = ' 0 ERROR\(S\)'
$partternWarn = ' 0 WARNING\(S\)'

Function analyseResult($result){
    $msg = $result[($result.Length - 1)]
    if($msg -notmatch $partternErr){
        echo $result
        exit
    }
    if($msg -notmatch $partternWarn){
        echo $result
    }
}

# 判断并创建目录
"build","bin" | ForEach-Object {
    if(!(Test-Path $_)){
        New-Item -ItemType Directory $_
    }
}

# 复制源文件
copy src\*.* build\

# 编译*.c文件
cd build
ls *.c | ForEach-Object {
    echo "build $_"
    $result = C51.EXE $_.Name BROWSE DEBUG OBJECTEXTEND
    analyseResult($result)
}
# 删除源文件
"*.c","*.h" | ForEach-Object {
    ls $_ | ForEach-Object {
        del $_.Name
    }
}

# 链接*.obj文件
$OBJ_PATH = "TEM"
ls *.obj | ForEach-Object {
    if($OBJ_PATH -eq "TEM"){
        $OBJ_PATH = "`"$_`""
    }else{
        $OBJ_PATH = $OBJ_PATH+",`"$_`""
    }
}
echo "link $OBJ_PATH"
$result = BL51.exe $OBJ_PATH TO "build.tmp"
analyseResult($result)
# 生成*.hex文件
echo "gen build.hex"
$result = OH51.exe build.tmp
#analyseResult($result)

# copy .hex to bin
cd ..
copy build\build.hex bin\target.hex
echo "`nSUCCESS"

# pause

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment