From 5c8e06ac7c787f5127a8a109c5964aac561bf071 Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 4 Jan 2023 22:46:10 +0800 Subject: add masm32 sdk --- tests/projects/asm/masm32/src/generic.asm | 332 +++++++++++++++++++++++++++++ tests/projects/asm/masm32/src/mainicon.ico | Bin 0 -> 766 bytes tests/projects/asm/masm32/src/rsrc.rc | 15 ++ tests/projects/asm/masm32/xmake.lua | 7 + xmake/modules/core/tools/rc.lua | 52 ++--- xmake/modules/detect/sdks/find_masm32.lua | 94 ++++++++ xmake/modules/detect/tools/find_link.lua | 6 +- xmake/modules/detect/tools/find_ml.lua | 2 - xmake/toolchains/masm32/xmake.lua | 50 +++++ 9 files changed, 527 insertions(+), 31 deletions(-) create mode 100644 tests/projects/asm/masm32/src/generic.asm create mode 100644 tests/projects/asm/masm32/src/mainicon.ico create mode 100644 tests/projects/asm/masm32/src/rsrc.rc create mode 100644 tests/projects/asm/masm32/xmake.lua create mode 100644 xmake/modules/detect/sdks/find_masm32.lua create mode 100644 xmake/toolchains/masm32/xmake.lua diff --git a/tests/projects/asm/masm32/src/generic.asm b/tests/projects/asm/masm32/src/generic.asm new file mode 100644 index 000000000..7d305e2ee --- /dev/null +++ b/tests/projects/asm/masm32/src/generic.asm @@ -0,0 +1,332 @@ +; ######################################################################### +; +; GENERIC.ASM is a roadmap around a standard 32 bit +; windows application skeleton written in MASM32. +; +; ######################################################################### + +; Assembler specific instructions for 32 bit ASM code + + .386 ; minimum processor needed for 32 bit + .model flat, stdcall ; FLAT memory model & STDCALL calling + option casemap :none ; set code to case sensitive + +; ######################################################################### + + ; --------------------------------------------- + ; main include file with equates and structures + ; --------------------------------------------- + include windows.inc + + ; ------------------------------------------------------------- + ; In MASM32, each include file created by the L2INC.EXE utility + ; has a matching library file. If you need functions from a + ; specific library, you use BOTH the include file and library + ; file for that library. + ; ------------------------------------------------------------- + + include user32.inc + include kernel32.inc + +; ######################################################################### + +; ------------------------------------------------------------------------ +; MACROS are a method of expanding text at assembly time. This allows the +; programmer a tidy and convenient way of using COMMON blocks of code with +; the capacity to use DIFFERENT parameters in each block. +; ------------------------------------------------------------------------ + + ; 1. szText + ; A macro to insert TEXT into the code section for convenient and + ; more intuitive coding of functions that use byte data as text. + + szText MACRO Name, Text:VARARG + LOCAL lbl + jmp lbl + Name db Text,0 + lbl: + ENDM + + ; 2. m2m + ; There is no mnemonic to copy from one memory location to another, + ; this macro saves repeated coding of this process and is easier to + ; read in complex code. + + m2m MACRO M1, M2 + push M2 + pop M1 + ENDM + + ; 3. return + ; Every procedure MUST have a "ret" to return the instruction + ; pointer EIP back to the next instruction after the call that + ; branched to it. This macro puts a return value in eax and + ; makes the "ret" instruction on one line. It is mainly used + ; for clear coding in complex conditionals in large branching + ; code such as the WndProc procedure. + + return MACRO arg + mov eax, arg + ret + ENDM + +; ######################################################################### + +; ---------------------------------------------------------------------- +; Prototypes are used in conjunction with the MASM "invoke" syntax for +; checking the number and size of parameters passed to a procedure. This +; improves the reliability of code that is written where errors in +; parameters are caught and displayed at assembly time. +; ---------------------------------------------------------------------- + + WinMain PROTO :DWORD,:DWORD,:DWORD,:DWORD + WndProc PROTO :DWORD,:DWORD,:DWORD,:DWORD + TopXY PROTO :DWORD,:DWORD + +; ######################################################################### + +; ------------------------------------------------------------------------ +; This is the INITIALISED data section meaning that data declared here has +; an initial value. You can also use an UNINIALISED section if you need +; data of that type [ .data? ]. Note that they are different and occur in +; different sections. +; ------------------------------------------------------------------------ + + .data + szDisplayName db "Generic",0 + CommandLine dd 0 + hWnd dd 0 + hInstance dd 0 + + + +; ######################################################################### + +; ------------------------------------------------------------------------ +; This is the start of the code section where executable code begins. This +; section ending with the ExitProcess() API function call is the only +; GLOBAL section of code and it provides access to the WinMain function +; with the necessary parameters, the instance handle and the command line +; address. +; ------------------------------------------------------------------------ + + .code + +; ----------------------------------------------------------------------- +; The label "start:" is the address of the start of the code section and +; it has a matching "end start" at the end of the file. All procedures in +; this module must be written between these two. +; ----------------------------------------------------------------------- + +start: + invoke GetModuleHandle, NULL ; provides the instance handle + mov hInstance, eax + + invoke GetCommandLine ; provides the command line address + mov CommandLine, eax + + invoke WinMain,hInstance,NULL,CommandLine,SW_SHOWDEFAULT + + invoke ExitProcess,eax ; cleanup & return to operating system + +; ######################################################################### + +WinMain proc hInst :DWORD, + hPrevInst :DWORD, + CmdLine :DWORD, + CmdShow :DWORD + + ;==================== + ; Put LOCALs on stack + ;==================== + + LOCAL wc :WNDCLASSEX + LOCAL msg :MSG + + LOCAL Wwd :DWORD + LOCAL Wht :DWORD + LOCAL Wtx :DWORD + LOCAL Wty :DWORD + + szText szClassName,"Generic_Class" + + ;================================================== + ; Fill WNDCLASSEX structure with required variables + ;================================================== + + mov wc.cbSize, sizeof WNDCLASSEX + mov wc.style, CS_HREDRAW or CS_VREDRAW \ + or CS_BYTEALIGNWINDOW + mov wc.lpfnWndProc, offset WndProc ; address of WndProc + mov wc.cbClsExtra, NULL + mov wc.cbWndExtra, NULL + m2m wc.hInstance, hInst ; instance handle + mov wc.hbrBackground, COLOR_BTNFACE+1 ; system color + mov wc.lpszMenuName, NULL + mov wc.lpszClassName, offset szClassName ; window class name + invoke LoadIcon,hInst,500 ; icon ID ; resource icon + mov wc.hIcon, eax + invoke LoadCursor,NULL,IDC_ARROW ; system cursor + mov wc.hCursor, eax + mov wc.hIconSm, 0 + + invoke RegisterClassEx, ADDR wc ; register the window class + + ;================================ + ; Centre window at following size + ;================================ + + mov Wwd, 500 + mov Wht, 350 + + invoke GetSystemMetrics,SM_CXSCREEN ; get screen width in pixels + invoke TopXY,Wwd,eax + mov Wtx, eax + + invoke GetSystemMetrics,SM_CYSCREEN ; get screen height in pixels + invoke TopXY,Wht,eax + mov Wty, eax + + ; ================================== + ; Create the main application window + ; ================================== + invoke CreateWindowEx,WS_EX_OVERLAPPEDWINDOW, + ADDR szClassName, + ADDR szDisplayName, + WS_OVERLAPPEDWINDOW, + Wtx,Wty,Wwd,Wht, + NULL,NULL, + hInst,NULL + + mov hWnd,eax ; copy return value into handle DWORD + + invoke LoadMenu,hInst,600 ; load resource menu + invoke SetMenu,hWnd,eax ; set it to main window + + invoke ShowWindow,hWnd,SW_SHOWNORMAL ; display the window + invoke UpdateWindow,hWnd ; update the display + + ;=================================== + ; Loop until PostQuitMessage is sent + ;=================================== + + StartLoop: + invoke GetMessage,ADDR msg,NULL,0,0 ; get each message + cmp eax, 0 ; exit if GetMessage() + je ExitLoop ; returns zero + invoke TranslateMessage, ADDR msg ; translate it + invoke DispatchMessage, ADDR msg ; send it to message proc + jmp StartLoop + ExitLoop: + + return msg.wParam + +WinMain endp + +; ######################################################################### + +WndProc proc hWin :DWORD, + uMsg :DWORD, + wParam :DWORD, + lParam :DWORD + +; ------------------------------------------------------------------------- +; Message are sent by the operating system to an application through the +; WndProc proc. Each message can have additional values associated with it +; in the two parameters, wParam & lParam. The range of additional data that +; can be passed to an application is determined by the message. +; ------------------------------------------------------------------------- + + .if uMsg == WM_COMMAND + ;---------------------------------------------------------------------- + ; The WM_COMMAND message is sent by menus, buttons and toolbar buttons. + ; Processing the wParam parameter of it is the method of obtaining the + ; control's ID number so that the code for each operation can be + ; processed. NOTE that the ID number is in the LOWORD of the wParam + ; passed with the WM_COMMAND message. There may be some instances where + ; an application needs to seperate the high and low words of wParam. + ; --------------------------------------------------------------------- + + ;======== menu commands ======== + + .if wParam == 1000 + invoke SendMessage,hWin,WM_SYSCOMMAND,SC_CLOSE,NULL + .elseif wParam == 1900 + szText TheMsg,"Assembler, Pure & Simple" + invoke MessageBox,hWin,ADDR TheMsg,ADDR szDisplayName,MB_OK + .endif + + ;====== end menu commands ====== + + .elseif uMsg == WM_CREATE + ; -------------------------------------------------------------------- + ; This message is sent to WndProc during the CreateWindowEx function + ; call and is processed before it returns. This is used as a position + ; to start other items such as controls. IMPORTANT, the handle for the + ; CreateWindowEx call in the WinMain does not yet exist so the HANDLE + ; passed to the WndProc [ hWin ] must be used here for any controls + ; or child windows. + ; -------------------------------------------------------------------- + + .elseif uMsg == WM_CLOSE + ; ------------------------------------------------------------------- + ; This is the place where various requirements are performed before + ; the application exits to the operating system such as deleting + ; resources and testing if files have been saved. You have the option + ; of returning ZERO if you don't wish the application to close which + ; exits the WndProc procedure without passing this message to the + ; default window processing done by the operating system. + ; ------------------------------------------------------------------- + szText TheText,"Please Confirm Exit" + invoke MessageBox,hWin,ADDR TheText,ADDR szDisplayName,MB_YESNO + .if eax == IDNO + return 0 + .endif + + .elseif uMsg == WM_DESTROY + ; ---------------------------------------------------------------- + ; This message MUST be processed to cleanly exit the application. + ; Calling the PostQuitMessage() function makes the GetMessage() + ; function in the WinMain() main loop return ZERO which exits the + ; application correctly. If this message is not processed properly + ; the window disappears but the code is left in memory. + ; ---------------------------------------------------------------- + invoke PostQuitMessage,NULL + return 0 + .endif + + invoke DefWindowProc,hWin,uMsg,wParam,lParam + ; -------------------------------------------------------------------- + ; Default window processing is done by the operating system for any + ; message that is not processed by the application in the WndProc + ; procedure. If the application requires other than default processing + ; it executes the code when the message is trapped and returns ZERO + ; to exit the WndProc procedure before the default window processing + ; occurs with the call to DefWindowProc(). + ; -------------------------------------------------------------------- + + ret + +WndProc endp + +; ######################################################################## + +TopXY proc wDim:DWORD, sDim:DWORD + + ; ---------------------------------------------------- + ; This procedure calculates the top X & Y co-ordinates + ; for the CreateWindowEx call in the WinMain procedure + ; ---------------------------------------------------- + + shr sDim, 1 ; divide screen dimension by 2 + shr wDim, 1 ; divide window dimension by 2 + mov eax, wDim ; copy window dimension into eax + sub sDim, eax ; sub half win dimension from half screen dimension + + return sDim + +TopXY endp + +; ######################################################################## + +end start diff --git a/tests/projects/asm/masm32/src/mainicon.ico b/tests/projects/asm/masm32/src/mainicon.ico new file mode 100644 index 000000000..945516553 Binary files /dev/null and b/tests/projects/asm/masm32/src/mainicon.ico differ diff --git a/tests/projects/asm/masm32/src/rsrc.rc b/tests/projects/asm/masm32/src/rsrc.rc new file mode 100644 index 000000000..67dd99dae --- /dev/null +++ b/tests/projects/asm/masm32/src/rsrc.rc @@ -0,0 +1,15 @@ +500 ICON MOVEABLE PURE LOADONCALL DISCARDABLE "MAINICON.ICO" + +600 MENUEX MOVEABLE IMPURE LOADONCALL DISCARDABLE +BEGIN + POPUP "&File", , , 0 + BEGIN + MENUITEM "&Exit", 1000 + END + POPUP "&Help", , , 0 + BEGIN + MENUITEM "&About", 1900 + END +END + + diff --git a/tests/projects/asm/masm32/xmake.lua b/tests/projects/asm/masm32/xmake.lua new file mode 100644 index 000000000..64f0f00b5 --- /dev/null +++ b/tests/projects/asm/masm32/xmake.lua @@ -0,0 +1,7 @@ +add_rules("mode.debug", "mode.release") +target("test") + set_kind("binary") + add_files("src/*.asm") + add_files("src/*.rc") + set_toolchains("masm32") + add_syslinks("user32", "kernel32") diff --git a/xmake/modules/core/tools/rc.lua b/xmake/modules/core/tools/rc.lua index 662a7b618..c3e5f5c12 100644 --- a/xmake/modules/core/tools/rc.lua +++ b/xmake/modules/core/tools/rc.lua @@ -122,35 +122,37 @@ function compile(self, sourcefile, objectfile, dependinfo, flags) } } - -- try to use cl.exe to parse includes + -- try to use cl.exe to parse includes, but cl.exe maybe not exists in masm32 sdk -- @see https://github.com/xmake-io/xmake/issues/2562 - local outfile = os.tmpfile() .. ".rc.out" - local errfile = os.tmpfile() .. ".rc.err" - local cl = assert(self:toolchain():tool("cxx"), "cl.exe not found!") - local ok = try {function () os.execv(cl, {"-E", sourcefile}, {stdout = outfile, stderr = errfile, envs = self:runenvs()}); return true end} - if ok and os.isfile(outfile) then - local depfiles_rc - local includeset = hashset.new() - local file = io.open(outfile) - local projectdir = os.projectdir() - for line in file:lines() do - local includefile = _parse_includefile(line) - if includefile then - includefile = _normailize_dep(includefile, projectdir) - if includefile and not includeset:has(includefile) - and path.absolute(includefile) ~= path.absolute(sourcefile) - and os.isfile(includefile) then - depfiles_rc = (depfiles_rc or "") .. "\n" .. includefile - includeset:insert(includefile) + local cl = self:toolchain():tool("cxx") + if cl then + local outfile = os.tmpfile() .. ".rc.out" + local errfile = os.tmpfile() .. ".rc.err" + local ok = try {function () os.execv(cl, {"-E", sourcefile}, {stdout = outfile, stderr = errfile, envs = self:runenvs()}); return true end} + if ok and os.isfile(outfile) then + local depfiles_rc + local includeset = hashset.new() + local file = io.open(outfile) + local projectdir = os.projectdir() + for line in file:lines() do + local includefile = _parse_includefile(line) + if includefile then + includefile = _normailize_dep(includefile, projectdir) + if includefile and not includeset:has(includefile) + and path.absolute(includefile) ~= path.absolute(sourcefile) + and os.isfile(includefile) then + depfiles_rc = (depfiles_rc or "") .. "\n" .. includefile + includeset:insert(includefile) + end end end + file:close() + if dependinfo then + dependinfo.depfiles_rc = depfiles_rc + end end - file:close() - if dependinfo then - dependinfo.depfiles_rc = depfiles_rc - end + os.tryrm(outfile) + os.tryrm(errfile) end - os.tryrm(outfile) - os.tryrm(errfile) end diff --git a/xmake/modules/detect/sdks/find_masm32.lua b/xmake/modules/detect/sdks/find_masm32.lua new file mode 100644 index 000000000..3b43e341c --- /dev/null +++ b/xmake/modules/detect/sdks/find_masm32.lua @@ -0,0 +1,94 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file find_masm32.lua +-- + +-- imports +import("lib.detect.find_path") +import("core.base.option") +import("core.project.config") +import("core.cache.detectcache") + +-- find masm32 directory +function _find_sdkdir(sdkdir) + local paths = {} + if sdkdir then + table.insert(paths, path.join(sdkdir, "bin")) + else + for _, logical_drive in ipairs(winos.logical_drives()) do + table.insert(paths, path.join(logical_drive, "masm32", "bin")) + end + end + local bindir = find_path("ml.exe", paths) + if bindir then + return path.directory(bindir) + end +end + +-- find masm32 toolchains +function _find_masm32(sdkdir) + sdkdir = _find_sdkdir(sdkdir) + if not sdkdir or not os.isdir(sdkdir) then + return + end + return {sdkdir = sdkdir, bindir = path.join(sdkdir, "bin"), includedir = path.join(sdkdir, "include"), libdir = path.join(sdkdir, "lib")} +end + +-- find masm32 toolchains +-- +-- @param sdkdir the masm32 directory +-- @param opt the argument options, e.g. {verbose = true, force = false} +-- +-- @return the masm32 toolchains. e.g. {sdkver = ..., sdkdir, sdkdir_armcc, sdkdir_armclang} +-- +-- @code +-- +-- local toolchains = find_masm32("~/masm32") +-- +-- @endcode +-- +function main(sdkdir, opt) + + -- init arguments + opt = opt or {} + + -- attempt to load cache first + local key = "detect.sdks.find_masm32" + local cacheinfo = detectcache:get(key) or {} + if not opt.force and cacheinfo.masm32 and cacheinfo.masm32.sdkdir and os.isdir(cacheinfo.masm32.sdkdir) then + return cacheinfo.masm32 + end + + -- find masm32 + local masm32 = _find_masm32(sdkdir or config.get("sdk")) + if masm32 then + if opt.verbose or option.get("verbose") then + cprint("checking for masm32 directory ... ${color.success}%s", masm32.sdkdir) + end + else + if opt.verbose or option.get("verbose") then + cprint("checking for masm32 directory ... ${color.nothing}${text.nothing}") + end + end + + -- save to cache + cacheinfo.masm32 = masm32 or false + detectcache:set(key, cacheinfo) + detectcache:save() + return masm32 +end diff --git a/xmake/modules/detect/tools/find_link.lua b/xmake/modules/detect/tools/find_link.lua index 0fcff08f0..0c2abae7b 100644 --- a/xmake/modules/detect/tools/find_link.lua +++ b/xmake/modules/detect/tools/find_link.lua @@ -39,9 +39,10 @@ function main(opt) -- init version info first local version = nil + local verinfo = nil -- init options - opt = opt or {} + opt = opt or {} opt.check = opt.check or function (program) -- find cl @@ -68,15 +69,12 @@ function main(opt) opt.parse = opt.parse or function (output) return output:match("Version (%d+%.?%d*%.?%d*.-)%s") end -- find program - local verinfo = nil local program = find_program(opt.program or "link.exe", opt) -- find program version if program and opt and opt.version then version = find_programver(program, opt) end - - -- ok? return program, version end diff --git a/xmake/modules/detect/tools/find_ml.lua b/xmake/modules/detect/tools/find_ml.lua index beeab79d7..75999f45a 100644 --- a/xmake/modules/detect/tools/find_ml.lua +++ b/xmake/modules/detect/tools/find_ml.lua @@ -50,8 +50,6 @@ function main(opt) opt.parse = opt.parse or function (output) return output:match("Version (%d+%.?%d*%.?%d*.-)%s") end version = find_programver(program, opt) end - - -- ok? return program, version end diff --git a/xmake/toolchains/masm32/xmake.lua b/xmake/toolchains/masm32/xmake.lua new file mode 100644 index 000000000..f0a1ac7a4 --- /dev/null +++ b/xmake/toolchains/masm32/xmake.lua @@ -0,0 +1,50 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +toolchain("masm32") + set_kind("standalone") + set_homepage("https://www.masm32.com") + set_description("The MASM32 SDK") + + set_toolset("as", "ml.exe") + set_toolset("mrc", "rc.exe") + set_toolset("ld", "link.exe") + set_toolset("sh", "link.exe") + set_toolset("ar", "link.exe") + + on_check(function (toolchain) + import("lib.detect.find_tool") + import("detect.sdks.find_masm32") + local masm32 = find_masm32() + if masm32 and masm32.sdkdir and masm32.bindir and find_tool("ml.exe", {program = path.join(masm32.bindir, "ml.exe")}) then + toolchain:config_set("sdkdir", masm32.sdkdir) + toolchain:config_set("bindir", masm32.bindir) + toolchain:configs_save() + return true + end + end) + + on_load(function (toolchain) + local sdkdir = toolchain:sdkdir() + if sdkdir then + toolchain:add("includedirs", path.join(sdkdir, "include")) + toolchain:add("linkdirs", path.join(sdkdir, "lib")) + end + end) -- cgit v1.3.1