main.cc (1978B)
1 // 2 // Copyright (c) 2016 Kyle Milz <kyle@0x30.net> 3 // 4 // Permission to use, copy, modify, and distribute this software for any 5 // purpose with or without fee is hereby granted, provided that the above 6 // copyright notice and this permission notice appear in all copies. 7 // 8 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 // 16 #ifdef _WIN32 17 #include "inst_fewin32.h" 18 19 #include <windows.h> 20 #include <Shlwapi.h> // PathFindFileNameA 21 #else /* _WIN32 */ 22 #include <err.h> 23 #include <libgen.h> // basename 24 25 #include "fe_unix.h" // InstFrontend 26 #endif /* _WIN32 */ 27 28 #include <cstring> // strcmp 29 30 31 int 32 main(int argc, char *argv[]) 33 { 34 char *base_name; 35 bool is_citrun_inst = false; 36 37 #ifdef _WIN32 38 // XXX: error checking 39 base_name = PathFindFileNameA(argv[0]); 40 #else // _WIN32 41 // Protect against argv[0] being an absolute path. 42 if ((base_name = basename(argv[0])) == NULL) 43 err(1, "basename"); 44 #endif // _WIN32 45 46 // Switch tool mode if we're called as 'citrun_inst'. 47 if ((std::strcmp(base_name, "citrun_inst") == 0) || 48 (std::strcmp(base_name, "citrun_inst.exe") == 0)) 49 is_citrun_inst = true; 50 51 // Always re-search PATH for binary name (in non citrun_inst case). 52 if (std::strcmp(argv[0], base_name) != 0) 53 argv[0] = base_name; 54 55 #ifdef _WIN32 56 InstFrontendWin32 main(argc, argv, is_citrun_inst); 57 #else 58 InstFrontendUnix main(argc, argv, is_citrun_inst); 59 #endif 60 61 main.log_identity(); 62 main.get_paths(); 63 main.clean_PATH(); 64 main.process_cmdline(); 65 66 main.instrument(); 67 main.compile_instrumented(); 68 69 return 0; 70 }