--
-- Link the kernel (CPU Driver)
--
linkKernel :: Options -> [String] -> [String] -> String -> HRule
linkKernel opts objs libs kbin =
    let linkscript = "/kernel/linker.lds"
        kbootable  = kbin ++ ".bin"
    in
        Rules [ Rule ([ Str compiler, Str Config.cOptFlags,
                      NStr "-T", In BuildTree arch linkscript,
                      Str "-o", Out arch kbin
                    ]
                    ++ (optLdFlags opts)
                    ++
                    [ In BuildTree arch o | o <- objs ]
                    ++
                    [ In BuildTree arch l | l <- libs ]
                    ++
                    [ Str "-lgcc" ]
                   ),
              -- Edit ELF header so qemu-system-arm will treat it as a Linux kernel
              Rule [ In SrcTree "src" "/tools/arm-mkbootelf.sh",
                     Str objdump, In BuildTree arch kbin, Out arch (kbootable)],
              -- Generate kernel assembly dump
              Rule [ Str (objdump ++ " -d -M reg-names-raw"),
                    In BuildTree arch kbin, Str ">", Out arch (kbin ++ ".asm")],
              Rule [ Str "cpp",
                     NStr "-I", NoDep SrcTree "src" "/kernel/include/arch/arm",
                     Str "-D__ASSEMBLER__",
                     Str "-P", In SrcTree "src" "/kernel/arch/arm/linker.lds.in",
                     Out arch linkscript
                   ]
            ]
linkKernel :: Options -> [String] -> [String] -> String -> HRule
linkKernel opts objs libs kbin =
    let linkscript = "/kernel/linker.lds"
        kbootable  = kbin ++ ".bin"
    in
        Rules [ Rule ([ Str compiler, Str Config.cOptFlags,
                      NStr "-T", In BuildTree arch linkscript,
                      Str "-o", Out arch kbin
                    ]
                    ++ (optLdFlags opts)
                    ++
                    [ In BuildTree arch o | o <- objs ]
                    ++
                    [ In BuildTree arch l | l <- libs ]
                    ++
                    [ Str "-lgcc" ]
                   ),
              -- Edit ELF header so qemu-system-arm will treat it as a Linux kernel
              Rule [ In SrcTree "src" "/tools/arm-mkbootelf.sh",
                     Str objdump, In BuildTree arch kbin, Out arch (kbootable)],
              -- Generate kernel assembly dump
              Rule [ Str (objdump ++ " -d -M reg-names-raw"),
                    In BuildTree arch kbin, Str ">", Out arch (kbin ++ ".asm")],
              Rule [ Str "cpp",
                     NStr "-I", NoDep SrcTree "src" "/kernel/include/arch/arm",
                     Str "-D__ASSEMBLER__",
                     Str "-P", In SrcTree "src" "/kernel/arch/arm/linker.lds.in",
                     Out arch linkscript
                   ]
            ]
linkKernel opts objs libs kbin =
    let linkscript = "/kernel/linker.lds"
        kbootable  = kbin ++ ".bin"
    in
        Rules [ Rule ([ Str compiler, Str Config.cOptFlags,
                      NStr "-T", In BuildTree arch linkscript,
                      Str "-o", Out arch kbin
                    ]
                    ++ (optLdFlags opts)
                    ++
                    [ In BuildTree arch o | o <- objs ]
                    ++
                    [ In BuildTree arch l | l <- libs ]
                    ++
                    [ Str "-lgcc" ]
                   ),
              -- Edit ELF header so qemu-system-arm will treat it as a Linux kernel
              Rule [ In SrcTree "src" "/tools/arm-mkbootelf.sh",
                     Str objdump, In BuildTree arch kbin, Out arch (kbootable)],
              -- Generate kernel assembly dump
              Rule [ Str (objdump ++ " -d -M reg-names-raw"),
                    In BuildTree arch kbin, Str ">", Out arch (kbin ++ ".asm")],
              Rule [ Str "cpp",
                     NStr "-I", NoDep SrcTree "src" "/kernel/include/arch/arm",
                     Str "-D__ASSEMBLER__",
                     Str "-P", In SrcTree "src" "/kernel/arch/arm/linker.lds.in",
                     Out arch linkscript
                   ]
            ]