makedep.c 150 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382
  1. /*
  2. * Generate include file dependencies
  3. *
  4. * Copyright 1996, 2013, 2020 Alexandre Julliard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #define NO_LIBWINE_PORT
  22. #include "wine/port.h"
  23. #include <assert.h>
  24. #include <ctype.h>
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <stdarg.h>
  29. #include <signal.h>
  30. #include <string.h>
  31. #ifdef HAVE_UNISTD_H
  32. # include <unistd.h>
  33. #endif
  34. #include "wine/list.h"
  35. struct strarray
  36. {
  37. unsigned int count; /* strings in use */
  38. unsigned int size; /* total allocated size */
  39. const char **str;
  40. };
  41. enum incl_type
  42. {
  43. INCL_NORMAL, /* #include "foo.h" */
  44. INCL_SYSTEM, /* #include <foo.h> */
  45. INCL_IMPORT, /* idl import "foo.idl" */
  46. INCL_IMPORTLIB, /* idl importlib "foo.tlb" */
  47. INCL_CPP_QUOTE, /* idl cpp_quote("#include \"foo.h\"") */
  48. INCL_CPP_QUOTE_SYSTEM /* idl cpp_quote("#include <foo.h>") */
  49. };
  50. struct dependency
  51. {
  52. int line; /* source line where this header is included */
  53. enum incl_type type; /* type of include */
  54. char *name; /* header name */
  55. };
  56. struct file
  57. {
  58. struct list entry;
  59. char *name; /* full file name relative to cwd */
  60. void *args; /* custom arguments for makefile rule */
  61. unsigned int flags; /* flags (see below) */
  62. unsigned int deps_count; /* files in use */
  63. unsigned int deps_size; /* total allocated size */
  64. struct dependency *deps; /* all header dependencies */
  65. };
  66. struct incl_file
  67. {
  68. struct list entry;
  69. struct file *file;
  70. char *name;
  71. char *filename;
  72. char *basename; /* base target name for generated files */
  73. char *sourcename; /* source file name for generated headers */
  74. struct incl_file *included_by; /* file that included this one */
  75. int included_line; /* line where this file was included */
  76. enum incl_type type; /* type of include */
  77. int use_msvcrt; /* put msvcrt headers in the search path? */
  78. struct incl_file *owner;
  79. unsigned int files_count; /* files in use */
  80. unsigned int files_size; /* total allocated size */
  81. struct incl_file **files;
  82. struct strarray dependencies; /* file dependencies */
  83. };
  84. #define FLAG_GENERATED 0x000001 /* generated file */
  85. #define FLAG_INSTALL 0x000002 /* file to install */
  86. #define FLAG_IDL_PROXY 0x000100 /* generates a proxy (_p.c) file */
  87. #define FLAG_IDL_CLIENT 0x000200 /* generates a client (_c.c) file */
  88. #define FLAG_IDL_SERVER 0x000400 /* generates a server (_s.c) file */
  89. #define FLAG_IDL_IDENT 0x000800 /* generates an ident (_i.c) file */
  90. #define FLAG_IDL_REGISTER 0x001000 /* generates a registration (_r.res) file */
  91. #define FLAG_IDL_TYPELIB 0x002000 /* generates a typelib (.tlb) file */
  92. #define FLAG_IDL_REGTYPELIB 0x004000 /* generates a registered typelib (_t.res) file */
  93. #define FLAG_IDL_HEADER 0x008000 /* generates a header (.h) file */
  94. #define FLAG_RC_PO 0x010000 /* rc file contains translations */
  95. #define FLAG_C_IMPLIB 0x020000 /* file is part of an import library */
  96. #define FLAG_C_UNIX 0x040000 /* file is part of a Unix library */
  97. #define FLAG_SFD_FONTS 0x080000 /* sfd file generated bitmap fonts */
  98. static const struct
  99. {
  100. unsigned int flag;
  101. const char *ext;
  102. } idl_outputs[] =
  103. {
  104. { FLAG_IDL_TYPELIB, ".tlb" },
  105. { FLAG_IDL_REGTYPELIB, "_t.res" },
  106. { FLAG_IDL_CLIENT, "_c.c" },
  107. { FLAG_IDL_IDENT, "_i.c" },
  108. { FLAG_IDL_PROXY, "_p.c" },
  109. { FLAG_IDL_SERVER, "_s.c" },
  110. { FLAG_IDL_REGISTER, "_r.res" },
  111. { FLAG_IDL_HEADER, ".h" }
  112. };
  113. #define HASH_SIZE 997
  114. static struct list files[HASH_SIZE];
  115. static const struct strarray empty_strarray;
  116. enum install_rules { INSTALL_LIB, INSTALL_DEV, NB_INSTALL_RULES };
  117. /* variables common to all makefiles */
  118. static struct strarray linguas;
  119. static struct strarray dll_flags;
  120. static struct strarray target_flags;
  121. static struct strarray msvcrt_flags;
  122. static struct strarray extra_cflags;
  123. static struct strarray extra_cross_cflags;
  124. static struct strarray cpp_flags;
  125. static struct strarray lddll_flags;
  126. static struct strarray libs;
  127. static struct strarray enable_tests;
  128. static struct strarray cmdline_vars;
  129. static struct strarray subdirs;
  130. static struct strarray disabled_dirs;
  131. static struct strarray cross_import_libs;
  132. static struct strarray delay_import_libs;
  133. static struct strarray top_install_lib;
  134. static struct strarray top_install_dev;
  135. static const char *root_src_dir;
  136. static const char *tools_dir;
  137. static const char *tools_ext;
  138. static const char *exe_ext;
  139. static const char *dll_ext;
  140. static const char *man_ext;
  141. static const char *crosstarget;
  142. static const char *crossdebug;
  143. static const char *fontforge;
  144. static const char *convert;
  145. static const char *flex;
  146. static const char *bison;
  147. static const char *ar;
  148. static const char *ranlib;
  149. static const char *rsvg;
  150. static const char *icotool;
  151. static const char *dlltool;
  152. static const char *msgfmt;
  153. static const char *ln_s;
  154. static const char *sed_cmd;
  155. static const char *delay_load_flag;
  156. struct makefile
  157. {
  158. /* values determined from input makefile */
  159. struct strarray vars;
  160. struct strarray include_paths;
  161. struct strarray include_args;
  162. struct strarray define_args;
  163. struct strarray programs;
  164. struct strarray scripts;
  165. struct strarray imports;
  166. struct strarray delayimports;
  167. struct strarray extradllflags;
  168. struct strarray install_lib;
  169. struct strarray install_dev;
  170. struct strarray extra_targets;
  171. struct strarray extra_imports;
  172. struct list sources;
  173. struct list includes;
  174. const char *src_dir;
  175. const char *obj_dir;
  176. const char *parent_dir;
  177. const char *module;
  178. const char *testdll;
  179. const char *sharedlib;
  180. const char *staticlib;
  181. const char *staticimplib;
  182. const char *importlib;
  183. int disabled;
  184. int use_msvcrt;
  185. int is_cross;
  186. int is_win16;
  187. int is_exe;
  188. /* values generated at output time */
  189. struct strarray in_files;
  190. struct strarray ok_files;
  191. struct strarray pot_files;
  192. struct strarray clean_files;
  193. struct strarray distclean_files;
  194. struct strarray uninstall_files;
  195. struct strarray object_files;
  196. struct strarray crossobj_files;
  197. struct strarray unixobj_files;
  198. struct strarray res_files;
  199. struct strarray font_files;
  200. struct strarray c2man_files;
  201. struct strarray debug_files;
  202. struct strarray dlldata_files;
  203. struct strarray implib_objs;
  204. struct strarray all_targets;
  205. struct strarray phony_targets;
  206. struct strarray dependencies;
  207. struct strarray install_rules[NB_INSTALL_RULES];
  208. };
  209. static struct makefile *top_makefile;
  210. static struct makefile **submakes;
  211. static const char separator[] = "### Dependencies";
  212. static const char *output_makefile_name = "Makefile";
  213. static const char *input_file_name;
  214. static const char *output_file_name;
  215. static const char *temp_file_name;
  216. static int relative_dir_mode;
  217. static int input_line;
  218. static int output_column;
  219. static FILE *output_file;
  220. static const char Usage[] =
  221. "Usage: makedep [options] [directories]\n"
  222. "Options:\n"
  223. " -R from to Compute the relative path between two directories\n"
  224. " -fxxx Store output in file 'xxx' (default: Makefile)\n";
  225. #ifndef __GNUC__
  226. #define __attribute__(x)
  227. #endif
  228. static void fatal_error( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
  229. static void fatal_perror( const char *msg, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
  230. static void output( const char *format, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
  231. static char *strmake( const char* fmt, ... ) __attribute__ ((__format__ (__printf__, 1, 2)));
  232. /*******************************************************************
  233. * fatal_error
  234. */
  235. static void fatal_error( const char *msg, ... )
  236. {
  237. va_list valist;
  238. va_start( valist, msg );
  239. if (input_file_name)
  240. {
  241. fprintf( stderr, "%s:", input_file_name );
  242. if (input_line) fprintf( stderr, "%d:", input_line );
  243. fprintf( stderr, " error: " );
  244. }
  245. else fprintf( stderr, "makedep: error: " );
  246. vfprintf( stderr, msg, valist );
  247. va_end( valist );
  248. exit(1);
  249. }
  250. /*******************************************************************
  251. * fatal_perror
  252. */
  253. static void fatal_perror( const char *msg, ... )
  254. {
  255. va_list valist;
  256. va_start( valist, msg );
  257. if (input_file_name)
  258. {
  259. fprintf( stderr, "%s:", input_file_name );
  260. if (input_line) fprintf( stderr, "%d:", input_line );
  261. fprintf( stderr, " error: " );
  262. }
  263. else fprintf( stderr, "makedep: error: " );
  264. vfprintf( stderr, msg, valist );
  265. perror( " " );
  266. va_end( valist );
  267. exit(1);
  268. }
  269. /*******************************************************************
  270. * cleanup_files
  271. */
  272. static void cleanup_files(void)
  273. {
  274. if (temp_file_name) unlink( temp_file_name );
  275. if (output_file_name) unlink( output_file_name );
  276. }
  277. /*******************************************************************
  278. * exit_on_signal
  279. */
  280. static void exit_on_signal( int sig )
  281. {
  282. exit( 1 ); /* this will call the atexit functions */
  283. }
  284. /*******************************************************************
  285. * xmalloc
  286. */
  287. static void *xmalloc( size_t size )
  288. {
  289. void *res;
  290. if (!(res = malloc (size ? size : 1)))
  291. fatal_error( "Virtual memory exhausted.\n" );
  292. return res;
  293. }
  294. /*******************************************************************
  295. * xrealloc
  296. */
  297. static void *xrealloc (void *ptr, size_t size)
  298. {
  299. void *res;
  300. assert( size );
  301. if (!(res = realloc( ptr, size )))
  302. fatal_error( "Virtual memory exhausted.\n" );
  303. return res;
  304. }
  305. /*******************************************************************
  306. * xstrdup
  307. */
  308. static char *xstrdup( const char *str )
  309. {
  310. char *res = strdup( str );
  311. if (!res) fatal_error( "Virtual memory exhausted.\n" );
  312. return res;
  313. }
  314. /*******************************************************************
  315. * strmake
  316. */
  317. static char *strmake( const char* fmt, ... )
  318. {
  319. int n;
  320. size_t size = 100;
  321. va_list ap;
  322. for (;;)
  323. {
  324. char *p = xmalloc (size);
  325. va_start(ap, fmt);
  326. n = vsnprintf (p, size, fmt, ap);
  327. va_end(ap);
  328. if (n == -1) size *= 2;
  329. else if ((size_t)n >= size) size = n + 1;
  330. else return xrealloc( p, n + 1 );
  331. free(p);
  332. }
  333. }
  334. /*******************************************************************
  335. * strendswith
  336. */
  337. static int strendswith( const char* str, const char* end )
  338. {
  339. size_t l = strlen( str );
  340. size_t m = strlen( end );
  341. return l >= m && strcmp(str + l - m, end) == 0;
  342. }
  343. /*******************************************************************
  344. * output
  345. */
  346. static void output( const char *format, ... )
  347. {
  348. int ret;
  349. va_list valist;
  350. va_start( valist, format );
  351. ret = vfprintf( output_file, format, valist );
  352. va_end( valist );
  353. if (ret < 0) fatal_perror( "output" );
  354. if (format[0] && format[strlen(format) - 1] == '\n') output_column = 0;
  355. else output_column += ret;
  356. }
  357. /*******************************************************************
  358. * strarray_add
  359. */
  360. static void strarray_add( struct strarray *array, const char *str )
  361. {
  362. if (array->count == array->size)
  363. {
  364. if (array->size) array->size *= 2;
  365. else array->size = 16;
  366. array->str = xrealloc( array->str, sizeof(array->str[0]) * array->size );
  367. }
  368. array->str[array->count++] = str;
  369. }
  370. /*******************************************************************
  371. * strarray_addall
  372. */
  373. static void strarray_addall( struct strarray *array, struct strarray added )
  374. {
  375. unsigned int i;
  376. for (i = 0; i < added.count; i++) strarray_add( array, added.str[i] );
  377. }
  378. /*******************************************************************
  379. * strarray_exists
  380. */
  381. static int strarray_exists( const struct strarray *array, const char *str )
  382. {
  383. unsigned int i;
  384. for (i = 0; i < array->count; i++) if (!strcmp( array->str[i], str )) return 1;
  385. return 0;
  386. }
  387. /*******************************************************************
  388. * strarray_add_uniq
  389. */
  390. static void strarray_add_uniq( struct strarray *array, const char *str )
  391. {
  392. if (!strarray_exists( array, str )) strarray_add( array, str );
  393. }
  394. /*******************************************************************
  395. * strarray_addall_uniq
  396. */
  397. static void strarray_addall_uniq( struct strarray *array, struct strarray added )
  398. {
  399. unsigned int i;
  400. for (i = 0; i < added.count; i++) strarray_add_uniq( array, added.str[i] );
  401. }
  402. /*******************************************************************
  403. * strarray_get_value
  404. *
  405. * Find a value in a name/value pair string array.
  406. */
  407. static const char *strarray_get_value( const struct strarray *array, const char *name )
  408. {
  409. int pos, res, min = 0, max = array->count / 2 - 1;
  410. while (min <= max)
  411. {
  412. pos = (min + max) / 2;
  413. if (!(res = strcmp( array->str[pos * 2], name ))) return array->str[pos * 2 + 1];
  414. if (res < 0) min = pos + 1;
  415. else max = pos - 1;
  416. }
  417. return NULL;
  418. }
  419. /*******************************************************************
  420. * strarray_set_value
  421. *
  422. * Define a value in a name/value pair string array.
  423. */
  424. static void strarray_set_value( struct strarray *array, const char *name, const char *value )
  425. {
  426. int i, pos, res, min = 0, max = array->count / 2 - 1;
  427. while (min <= max)
  428. {
  429. pos = (min + max) / 2;
  430. if (!(res = strcmp( array->str[pos * 2], name )))
  431. {
  432. /* redefining a variable replaces the previous value */
  433. array->str[pos * 2 + 1] = value;
  434. return;
  435. }
  436. if (res < 0) min = pos + 1;
  437. else max = pos - 1;
  438. }
  439. strarray_add( array, NULL );
  440. strarray_add( array, NULL );
  441. for (i = array->count - 1; i > min * 2 + 1; i--) array->str[i] = array->str[i - 2];
  442. array->str[min * 2] = name;
  443. array->str[min * 2 + 1] = value;
  444. }
  445. /*******************************************************************
  446. * strarray_set_qsort
  447. */
  448. static void strarray_qsort( struct strarray *array, int (*func)(const char **, const char **) )
  449. {
  450. if (array->count) qsort( array->str, array->count, sizeof(*array->str), (void *)func );
  451. }
  452. /*******************************************************************
  453. * output_filename
  454. */
  455. static void output_filename( const char *name )
  456. {
  457. if (output_column + strlen(name) + 1 > 100)
  458. {
  459. output( " \\\n" );
  460. output( " " );
  461. }
  462. else if (output_column) output( " " );
  463. output( "%s", name );
  464. }
  465. /*******************************************************************
  466. * output_filenames
  467. */
  468. static void output_filenames( struct strarray array )
  469. {
  470. unsigned int i;
  471. for (i = 0; i < array.count; i++) output_filename( array.str[i] );
  472. }
  473. /*******************************************************************
  474. * output_rm_filenames
  475. */
  476. static void output_rm_filenames( struct strarray array )
  477. {
  478. static const unsigned int max_cmdline = 30000; /* to be on the safe side */
  479. unsigned int i, len;
  480. if (!array.count) return;
  481. output( "\trm -f" );
  482. for (i = len = 0; i < array.count; i++)
  483. {
  484. if (len > max_cmdline)
  485. {
  486. output( "\n" );
  487. output( "\trm -f" );
  488. len = 0;
  489. }
  490. output_filename( array.str[i] );
  491. len += strlen( array.str[i] ) + 1;
  492. }
  493. output( "\n" );
  494. }
  495. /*******************************************************************
  496. * get_extension
  497. */
  498. static char *get_extension( char *filename )
  499. {
  500. char *ext = strrchr( filename, '.' );
  501. if (ext && strchr( ext, '/' )) ext = NULL;
  502. return ext;
  503. }
  504. /*******************************************************************
  505. * get_base_name
  506. */
  507. static const char *get_base_name( const char *name )
  508. {
  509. char *base;
  510. if (!strchr( name, '.' )) return name;
  511. base = strdup( name );
  512. *strrchr( base, '.' ) = 0;
  513. return base;
  514. }
  515. /*******************************************************************
  516. * replace_extension
  517. */
  518. static char *replace_extension( const char *name, const char *old_ext, const char *new_ext )
  519. {
  520. char *ret;
  521. size_t name_len = strlen( name );
  522. size_t ext_len = strlen( old_ext );
  523. if (name_len >= ext_len && !strcmp( name + name_len - ext_len, old_ext )) name_len -= ext_len;
  524. ret = xmalloc( name_len + strlen( new_ext ) + 1 );
  525. memcpy( ret, name, name_len );
  526. strcpy( ret + name_len, new_ext );
  527. return ret;
  528. }
  529. /*******************************************************************
  530. * replace_filename
  531. */
  532. static char *replace_filename( const char *path, const char *name )
  533. {
  534. const char *p;
  535. char *ret;
  536. size_t len;
  537. if (!path) return xstrdup( name );
  538. if (!(p = strrchr( path, '/' ))) return xstrdup( name );
  539. len = p - path + 1;
  540. ret = xmalloc( len + strlen( name ) + 1 );
  541. memcpy( ret, path, len );
  542. strcpy( ret + len, name );
  543. return ret;
  544. }
  545. /*******************************************************************
  546. * strarray_replace_extension
  547. */
  548. static struct strarray strarray_replace_extension( const struct strarray *array,
  549. const char *old_ext, const char *new_ext )
  550. {
  551. unsigned int i;
  552. struct strarray ret;
  553. ret.count = ret.size = array->count;
  554. ret.str = xmalloc( sizeof(ret.str[0]) * ret.size );
  555. for (i = 0; i < array->count; i++) ret.str[i] = replace_extension( array->str[i], old_ext, new_ext );
  556. return ret;
  557. }
  558. /*******************************************************************
  559. * replace_substr
  560. */
  561. static char *replace_substr( const char *str, const char *start, size_t len, const char *replace )
  562. {
  563. size_t pos = start - str;
  564. char *ret = xmalloc( pos + strlen(replace) + strlen(start + len) + 1 );
  565. memcpy( ret, str, pos );
  566. strcpy( ret + pos, replace );
  567. strcat( ret + pos, start + len );
  568. return ret;
  569. }
  570. /*******************************************************************
  571. * get_relative_path
  572. *
  573. * Determine where the destination path is located relative to the 'from' path.
  574. */
  575. static char *get_relative_path( const char *from, const char *dest )
  576. {
  577. const char *start;
  578. char *ret, *p;
  579. unsigned int dotdots = 0;
  580. /* a path of "." is equivalent to an empty path */
  581. if (!strcmp( from, "." )) from = "";
  582. for (;;)
  583. {
  584. while (*from == '/') from++;
  585. while (*dest == '/') dest++;
  586. start = dest; /* save start of next path element */
  587. if (!*from) break;
  588. while (*from && *from != '/' && *from == *dest) { from++; dest++; }
  589. if ((!*from || *from == '/') && (!*dest || *dest == '/')) continue;
  590. /* count remaining elements in 'from' */
  591. do
  592. {
  593. dotdots++;
  594. while (*from && *from != '/') from++;
  595. while (*from == '/') from++;
  596. }
  597. while (*from);
  598. break;
  599. }
  600. if (!start[0] && !dotdots) return NULL; /* empty path */
  601. ret = xmalloc( 3 * dotdots + strlen( start ) + 1 );
  602. for (p = ret; dotdots; dotdots--, p += 3) memcpy( p, "../", 3 );
  603. if (start[0]) strcpy( p, start );
  604. else p[-1] = 0; /* remove trailing slash */
  605. return ret;
  606. }
  607. /*******************************************************************
  608. * concat_paths
  609. */
  610. static char *concat_paths( const char *base, const char *path )
  611. {
  612. int i, len;
  613. char *ret;
  614. if (!base || !base[0]) return xstrdup( path && path[0] ? path : "." );
  615. if (!path || !path[0]) return xstrdup( base );
  616. if (path[0] == '/') return xstrdup( path );
  617. len = strlen( base );
  618. while (len && base[len - 1] == '/') len--;
  619. while (len && !strncmp( path, "..", 2 ) && (!path[2] || path[2] == '/'))
  620. {
  621. for (i = len; i > 0; i--) if (base[i - 1] == '/') break;
  622. if (i == len - 2 && !memcmp( base + i, "..", 2 )) break; /* we can't go up if we already have ".." */
  623. if (i != len - 1 || base[i] != '.')
  624. {
  625. path += 2;
  626. while (*path == '/') path++;
  627. }
  628. /* else ignore "." element */
  629. while (i > 0 && base[i - 1] == '/') i--;
  630. len = i;
  631. }
  632. if (!len && base[0] != '/') return xstrdup( path[0] ? path : "." );
  633. ret = xmalloc( len + strlen( path ) + 2 );
  634. memcpy( ret, base, len );
  635. ret[len++] = '/';
  636. strcpy( ret + len, path );
  637. return ret;
  638. }
  639. /*******************************************************************
  640. * obj_dir_path
  641. */
  642. static char *obj_dir_path( const struct makefile *make, const char *path )
  643. {
  644. return concat_paths( make->obj_dir, path );
  645. }
  646. /*******************************************************************
  647. * src_dir_path
  648. */
  649. static char *src_dir_path( const struct makefile *make, const char *path )
  650. {
  651. if (make->src_dir) return concat_paths( make->src_dir, path );
  652. return obj_dir_path( make, path );
  653. }
  654. /*******************************************************************
  655. * root_src_dir_path
  656. */
  657. static char *root_src_dir_path( const char *path )
  658. {
  659. return concat_paths( root_src_dir, path );
  660. }
  661. /*******************************************************************
  662. * tools_dir_path
  663. */
  664. static char *tools_dir_path( const struct makefile *make, const char *path )
  665. {
  666. if (tools_dir) return strmake( "%s/tools/%s", tools_dir, path );
  667. return strmake( "tools/%s", path );
  668. }
  669. /*******************************************************************
  670. * tools_path
  671. */
  672. static char *tools_path( const struct makefile *make, const char *name )
  673. {
  674. return strmake( "%s/%s%s", tools_dir_path( make, name ), name, tools_ext );
  675. }
  676. /*******************************************************************
  677. * strarray_addall_path
  678. */
  679. static void strarray_addall_path( struct strarray *array, const char *dir, struct strarray added )
  680. {
  681. unsigned int i;
  682. for (i = 0; i < added.count; i++) strarray_add( array, concat_paths( dir, added.str[i] ));
  683. }
  684. /*******************************************************************
  685. * get_line
  686. */
  687. static char *get_line( FILE *file )
  688. {
  689. static char *buffer;
  690. static size_t size;
  691. if (!size)
  692. {
  693. size = 1024;
  694. buffer = xmalloc( size );
  695. }
  696. if (!fgets( buffer, size, file )) return NULL;
  697. input_line++;
  698. for (;;)
  699. {
  700. char *p = buffer + strlen(buffer);
  701. /* if line is larger than buffer, resize buffer */
  702. while (p == buffer + size - 1 && p[-1] != '\n')
  703. {
  704. buffer = xrealloc( buffer, size * 2 );
  705. if (!fgets( buffer + size - 1, size + 1, file )) break;
  706. p = buffer + strlen(buffer);
  707. size *= 2;
  708. }
  709. if (p > buffer && p[-1] == '\n')
  710. {
  711. *(--p) = 0;
  712. if (p > buffer && p[-1] == '\r') *(--p) = 0;
  713. if (p > buffer && p[-1] == '\\')
  714. {
  715. *(--p) = 0;
  716. /* line ends in backslash, read continuation line */
  717. if (!fgets( p, size - (p - buffer), file )) return buffer;
  718. input_line++;
  719. continue;
  720. }
  721. }
  722. return buffer;
  723. }
  724. }
  725. /*******************************************************************
  726. * hash_filename
  727. */
  728. static unsigned int hash_filename( const char *name )
  729. {
  730. /* FNV-1 hash */
  731. unsigned int ret = 2166136261u;
  732. while (*name) ret = (ret * 16777619) ^ *name++;
  733. return ret % HASH_SIZE;
  734. }
  735. /*******************************************************************
  736. * add_file
  737. */
  738. static struct file *add_file( const char *name )
  739. {
  740. struct file *file = xmalloc( sizeof(*file) );
  741. memset( file, 0, sizeof(*file) );
  742. file->name = xstrdup( name );
  743. return file;
  744. }
  745. /*******************************************************************
  746. * add_dependency
  747. */
  748. static void add_dependency( struct file *file, const char *name, enum incl_type type )
  749. {
  750. /* enforce some rules for the Wine tree */
  751. if (!memcmp( name, "../", 3 ))
  752. fatal_error( "#include directive with relative path not allowed\n" );
  753. if (!strcmp( name, "config.h" ))
  754. {
  755. if (strendswith( file->name, ".h" ))
  756. fatal_error( "config.h must not be included by a header file\n" );
  757. if (file->deps_count)
  758. fatal_error( "config.h must be included before anything else\n" );
  759. }
  760. else if (!strcmp( name, "wine/port.h" ))
  761. {
  762. if (strendswith( file->name, ".h" ))
  763. fatal_error( "wine/port.h must not be included by a header file\n" );
  764. if (!file->deps_count) fatal_error( "config.h must be included before wine/port.h\n" );
  765. if (file->deps_count > 1)
  766. fatal_error( "wine/port.h must be included before everything except config.h\n" );
  767. if (strcmp( file->deps[0].name, "config.h" ))
  768. fatal_error( "config.h must be included before wine/port.h\n" );
  769. }
  770. if (file->deps_count >= file->deps_size)
  771. {
  772. file->deps_size *= 2;
  773. if (file->deps_size < 16) file->deps_size = 16;
  774. file->deps = xrealloc( file->deps, file->deps_size * sizeof(*file->deps) );
  775. }
  776. file->deps[file->deps_count].line = input_line;
  777. file->deps[file->deps_count].type = type;
  778. file->deps[file->deps_count].name = xstrdup( name );
  779. file->deps_count++;
  780. }
  781. /*******************************************************************
  782. * find_src_file
  783. */
  784. static struct incl_file *find_src_file( const struct makefile *make, const char *name )
  785. {
  786. struct incl_file *file;
  787. LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry )
  788. if (!strcmp( name, file->name )) return file;
  789. return NULL;
  790. }
  791. /*******************************************************************
  792. * find_include_file
  793. */
  794. static struct incl_file *find_include_file( const struct makefile *make, const char *name )
  795. {
  796. struct incl_file *file;
  797. LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry )
  798. if (!strcmp( name, file->name )) return file;
  799. return NULL;
  800. }
  801. /*******************************************************************
  802. * add_include
  803. *
  804. * Add an include file if it doesn't already exists.
  805. */
  806. static struct incl_file *add_include( struct makefile *make, struct incl_file *parent,
  807. const char *name, int line, enum incl_type type )
  808. {
  809. struct incl_file *include;
  810. if (parent->files_count >= parent->files_size)
  811. {
  812. parent->files_size *= 2;
  813. if (parent->files_size < 16) parent->files_size = 16;
  814. parent->files = xrealloc( parent->files, parent->files_size * sizeof(*parent->files) );
  815. }
  816. LIST_FOR_EACH_ENTRY( include, &make->includes, struct incl_file, entry )
  817. if (!parent->use_msvcrt == !include->use_msvcrt && !strcmp( name, include->name ))
  818. goto found;
  819. include = xmalloc( sizeof(*include) );
  820. memset( include, 0, sizeof(*include) );
  821. include->name = xstrdup(name);
  822. include->included_by = parent;
  823. include->included_line = line;
  824. include->type = type;
  825. include->use_msvcrt = parent->use_msvcrt;
  826. list_add_tail( &make->includes, &include->entry );
  827. found:
  828. parent->files[parent->files_count++] = include;
  829. return include;
  830. }
  831. /*******************************************************************
  832. * add_generated_source
  833. *
  834. * Add a generated source file to the list.
  835. */
  836. static struct incl_file *add_generated_source( struct makefile *make,
  837. const char *name, const char *filename )
  838. {
  839. struct incl_file *file;
  840. if ((file = find_src_file( make, name ))) return file; /* we already have it */
  841. file = xmalloc( sizeof(*file) );
  842. memset( file, 0, sizeof(*file) );
  843. file->file = add_file( name );
  844. file->name = xstrdup( name );
  845. file->basename = xstrdup( filename ? filename : name );
  846. file->filename = obj_dir_path( make, file->basename );
  847. file->file->flags = FLAG_GENERATED;
  848. file->use_msvcrt = make->use_msvcrt;
  849. list_add_tail( &make->sources, &file->entry );
  850. return file;
  851. }
  852. /*******************************************************************
  853. * parse_include_directive
  854. */
  855. static void parse_include_directive( struct file *source, char *str )
  856. {
  857. char quote, *include, *p = str;
  858. while (*p && isspace(*p)) p++;
  859. if (*p != '\"' && *p != '<' ) return;
  860. quote = *p++;
  861. if (quote == '<') quote = '>';
  862. include = p;
  863. while (*p && (*p != quote)) p++;
  864. if (!*p) fatal_error( "malformed include directive '%s'\n", str );
  865. *p = 0;
  866. add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
  867. }
  868. /*******************************************************************
  869. * parse_pragma_directive
  870. */
  871. static void parse_pragma_directive( struct file *source, char *str )
  872. {
  873. char *flag, *p = str;
  874. if (!isspace( *p )) return;
  875. while (*p && isspace(*p)) p++;
  876. p = strtok( p, " \t" );
  877. if (strcmp( p, "makedep" )) return;
  878. while ((flag = strtok( NULL, " \t" )))
  879. {
  880. if (!strcmp( flag, "depend" ))
  881. {
  882. while ((p = strtok( NULL, " \t" ))) add_dependency( source, p, INCL_NORMAL );
  883. return;
  884. }
  885. else if (!strcmp( flag, "install" )) source->flags |= FLAG_INSTALL;
  886. if (strendswith( source->name, ".idl" ))
  887. {
  888. if (!strcmp( flag, "header" )) source->flags |= FLAG_IDL_HEADER;
  889. else if (!strcmp( flag, "proxy" )) source->flags |= FLAG_IDL_PROXY;
  890. else if (!strcmp( flag, "client" )) source->flags |= FLAG_IDL_CLIENT;
  891. else if (!strcmp( flag, "server" )) source->flags |= FLAG_IDL_SERVER;
  892. else if (!strcmp( flag, "ident" )) source->flags |= FLAG_IDL_IDENT;
  893. else if (!strcmp( flag, "typelib" )) source->flags |= FLAG_IDL_TYPELIB;
  894. else if (!strcmp( flag, "register" )) source->flags |= FLAG_IDL_REGISTER;
  895. else if (!strcmp( flag, "regtypelib" )) source->flags |= FLAG_IDL_REGTYPELIB;
  896. }
  897. else if (strendswith( source->name, ".rc" ))
  898. {
  899. if (!strcmp( flag, "po" )) source->flags |= FLAG_RC_PO;
  900. }
  901. else if (strendswith( source->name, ".sfd" ))
  902. {
  903. if (!strcmp( flag, "font" ))
  904. {
  905. struct strarray *array = source->args;
  906. if (!array)
  907. {
  908. source->args = array = xmalloc( sizeof(*array) );
  909. *array = empty_strarray;
  910. source->flags |= FLAG_SFD_FONTS;
  911. }
  912. strarray_add( array, xstrdup( strtok( NULL, "" )));
  913. return;
  914. }
  915. }
  916. else
  917. {
  918. if (!strcmp( flag, "implib" )) source->flags |= FLAG_C_IMPLIB;
  919. if (!strcmp( flag, "unix" )) source->flags |= FLAG_C_UNIX;
  920. }
  921. }
  922. }
  923. /*******************************************************************
  924. * parse_cpp_directive
  925. */
  926. static void parse_cpp_directive( struct file *source, char *str )
  927. {
  928. while (*str && isspace(*str)) str++;
  929. if (*str++ != '#') return;
  930. while (*str && isspace(*str)) str++;
  931. if (!strncmp( str, "include", 7 ))
  932. parse_include_directive( source, str + 7 );
  933. else if (!strncmp( str, "import", 6 ) && strendswith( source->name, ".m" ))
  934. parse_include_directive( source, str + 6 );
  935. else if (!strncmp( str, "pragma", 6 ))
  936. parse_pragma_directive( source, str + 6 );
  937. }
  938. /*******************************************************************
  939. * parse_idl_file
  940. */
  941. static void parse_idl_file( struct file *source, FILE *file )
  942. {
  943. char *buffer, *include;
  944. input_line = 0;
  945. while ((buffer = get_line( file )))
  946. {
  947. char quote;
  948. char *p = buffer;
  949. while (*p && isspace(*p)) p++;
  950. if (!strncmp( p, "importlib", 9 ))
  951. {
  952. p += 9;
  953. while (*p && isspace(*p)) p++;
  954. if (*p++ != '(') continue;
  955. while (*p && isspace(*p)) p++;
  956. if (*p++ != '"') continue;
  957. include = p;
  958. while (*p && (*p != '"')) p++;
  959. if (!*p) fatal_error( "malformed importlib directive\n" );
  960. *p = 0;
  961. add_dependency( source, include, INCL_IMPORTLIB );
  962. continue;
  963. }
  964. if (!strncmp( p, "import", 6 ))
  965. {
  966. p += 6;
  967. while (*p && isspace(*p)) p++;
  968. if (*p != '"') continue;
  969. include = ++p;
  970. while (*p && (*p != '"')) p++;
  971. if (!*p) fatal_error( "malformed import directive\n" );
  972. *p = 0;
  973. add_dependency( source, include, INCL_IMPORT );
  974. continue;
  975. }
  976. /* check for #include inside cpp_quote */
  977. if (!strncmp( p, "cpp_quote", 9 ))
  978. {
  979. p += 9;
  980. while (*p && isspace(*p)) p++;
  981. if (*p++ != '(') continue;
  982. while (*p && isspace(*p)) p++;
  983. if (*p++ != '"') continue;
  984. if (*p++ != '#') continue;
  985. while (*p && isspace(*p)) p++;
  986. if (strncmp( p, "include", 7 )) continue;
  987. p += 7;
  988. while (*p && isspace(*p)) p++;
  989. if (*p == '\\' && p[1] == '"')
  990. {
  991. p += 2;
  992. quote = '"';
  993. }
  994. else
  995. {
  996. if (*p++ != '<' ) continue;
  997. quote = '>';
  998. }
  999. include = p;
  1000. while (*p && (*p != quote)) p++;
  1001. if (!*p || (quote == '"' && p[-1] != '\\'))
  1002. fatal_error( "malformed #include directive inside cpp_quote\n" );
  1003. if (quote == '"') p--; /* remove backslash */
  1004. *p = 0;
  1005. add_dependency( source, include, (quote == '>') ? INCL_CPP_QUOTE_SYSTEM : INCL_CPP_QUOTE );
  1006. continue;
  1007. }
  1008. parse_cpp_directive( source, p );
  1009. }
  1010. }
  1011. /*******************************************************************
  1012. * parse_c_file
  1013. */
  1014. static void parse_c_file( struct file *source, FILE *file )
  1015. {
  1016. char *buffer;
  1017. input_line = 0;
  1018. while ((buffer = get_line( file )))
  1019. {
  1020. parse_cpp_directive( source, buffer );
  1021. }
  1022. }
  1023. /*******************************************************************
  1024. * parse_rc_file
  1025. */
  1026. static void parse_rc_file( struct file *source, FILE *file )
  1027. {
  1028. char *buffer, *include;
  1029. input_line = 0;
  1030. while ((buffer = get_line( file )))
  1031. {
  1032. char quote;
  1033. char *p = buffer;
  1034. while (*p && isspace(*p)) p++;
  1035. if (p[0] == '/' && p[1] == '*') /* check for magic makedep comment */
  1036. {
  1037. p += 2;
  1038. while (*p && isspace(*p)) p++;
  1039. if (strncmp( p, "@makedep:", 9 )) continue;
  1040. p += 9;
  1041. while (*p && isspace(*p)) p++;
  1042. quote = '"';
  1043. if (*p == quote)
  1044. {
  1045. include = ++p;
  1046. while (*p && *p != quote) p++;
  1047. }
  1048. else
  1049. {
  1050. include = p;
  1051. while (*p && !isspace(*p) && *p != '*') p++;
  1052. }
  1053. if (!*p)
  1054. fatal_error( "malformed makedep comment\n" );
  1055. *p = 0;
  1056. add_dependency( source, include, (quote == '>') ? INCL_SYSTEM : INCL_NORMAL );
  1057. continue;
  1058. }
  1059. parse_cpp_directive( source, buffer );
  1060. }
  1061. }
  1062. /*******************************************************************
  1063. * parse_in_file
  1064. */
  1065. static void parse_in_file( struct file *source, FILE *file )
  1066. {
  1067. char *p, *buffer;
  1068. /* make sure it gets rebuilt when the version changes */
  1069. add_dependency( source, "config.h", INCL_SYSTEM );
  1070. if (!strendswith( source->name, ".man.in" )) return; /* not a man page */
  1071. input_line = 0;
  1072. while ((buffer = get_line( file )))
  1073. {
  1074. if (strncmp( buffer, ".TH", 3 )) continue;
  1075. if (!(p = strtok( buffer, " \t" ))) continue; /* .TH */
  1076. if (!(p = strtok( NULL, " \t" ))) continue; /* program name */
  1077. if (!(p = strtok( NULL, " \t" ))) continue; /* man section */
  1078. source->args = xstrdup( p );
  1079. return;
  1080. }
  1081. }
  1082. /*******************************************************************
  1083. * parse_sfd_file
  1084. */
  1085. static void parse_sfd_file( struct file *source, FILE *file )
  1086. {
  1087. char *p, *eol, *buffer;
  1088. input_line = 0;
  1089. while ((buffer = get_line( file )))
  1090. {
  1091. if (strncmp( buffer, "UComments:", 10 )) continue;
  1092. p = buffer + 10;
  1093. while (*p == ' ') p++;
  1094. if (p[0] == '"' && p[1] && buffer[strlen(buffer) - 1] == '"')
  1095. {
  1096. p++;
  1097. buffer[strlen(buffer) - 1] = 0;
  1098. }
  1099. while ((eol = strstr( p, "+AAoA" )))
  1100. {
  1101. *eol = 0;
  1102. while (*p && isspace(*p)) p++;
  1103. if (*p++ == '#')
  1104. {
  1105. while (*p && isspace(*p)) p++;
  1106. if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
  1107. }
  1108. p = eol + 5;
  1109. }
  1110. while (*p && isspace(*p)) p++;
  1111. if (*p++ != '#') return;
  1112. while (*p && isspace(*p)) p++;
  1113. if (!strncmp( p, "pragma", 6 )) parse_pragma_directive( source, p + 6 );
  1114. return;
  1115. }
  1116. }
  1117. static const struct
  1118. {
  1119. const char *ext;
  1120. void (*parse)( struct file *file, FILE *f );
  1121. } parse_functions[] =
  1122. {
  1123. { ".c", parse_c_file },
  1124. { ".h", parse_c_file },
  1125. { ".inl", parse_c_file },
  1126. { ".l", parse_c_file },
  1127. { ".m", parse_c_file },
  1128. { ".rh", parse_c_file },
  1129. { ".x", parse_c_file },
  1130. { ".y", parse_c_file },
  1131. { ".idl", parse_idl_file },
  1132. { ".rc", parse_rc_file },
  1133. { ".in", parse_in_file },
  1134. { ".sfd", parse_sfd_file }
  1135. };
  1136. /*******************************************************************
  1137. * load_file
  1138. */
  1139. static struct file *load_file( const char *name )
  1140. {
  1141. struct file *file;
  1142. FILE *f;
  1143. unsigned int i, hash = hash_filename( name );
  1144. LIST_FOR_EACH_ENTRY( file, &files[hash], struct file, entry )
  1145. if (!strcmp( name, file->name )) return file;
  1146. if (!(f = fopen( name, "r" ))) return NULL;
  1147. file = add_file( name );
  1148. list_add_tail( &files[hash], &file->entry );
  1149. input_file_name = file->name;
  1150. input_line = 0;
  1151. for (i = 0; i < sizeof(parse_functions) / sizeof(parse_functions[0]); i++)
  1152. {
  1153. if (!strendswith( name, parse_functions[i].ext )) continue;
  1154. parse_functions[i].parse( file, f );
  1155. break;
  1156. }
  1157. fclose( f );
  1158. input_file_name = NULL;
  1159. return file;
  1160. }
  1161. /*******************************************************************
  1162. * open_include_path_file
  1163. *
  1164. * Open a file from a directory on the include path.
  1165. */
  1166. static struct file *open_include_path_file( const struct makefile *make, const char *dir,
  1167. const char *name, char **filename )
  1168. {
  1169. char *src_path = concat_paths( dir, name );
  1170. struct file *ret = load_file( src_path );
  1171. if (ret) *filename = src_path;
  1172. return ret;
  1173. }
  1174. /*******************************************************************
  1175. * open_file_same_dir
  1176. *
  1177. * Open a file in the same directory as the parent.
  1178. */
  1179. static struct file *open_file_same_dir( const struct incl_file *parent, const char *name, char **filename )
  1180. {
  1181. char *src_path = replace_filename( parent->file->name, name );
  1182. struct file *ret = load_file( src_path );
  1183. if (ret) *filename = replace_filename( parent->filename, name );
  1184. return ret;
  1185. }
  1186. /*******************************************************************
  1187. * open_local_file
  1188. *
  1189. * Open a file in the source directory of the makefile.
  1190. */
  1191. static struct file *open_local_file( const struct makefile *make, const char *path, char **filename )
  1192. {
  1193. char *src_path = src_dir_path( make, path );
  1194. struct file *ret = load_file( src_path );
  1195. /* if not found, try parent dir */
  1196. if (!ret && make->parent_dir)
  1197. {
  1198. free( src_path );
  1199. path = strmake( "%s/%s", make->parent_dir, path );
  1200. src_path = src_dir_path( make, path );
  1201. ret = load_file( src_path );
  1202. }
  1203. if (ret) *filename = src_path;
  1204. return ret;
  1205. }
  1206. /*******************************************************************
  1207. * open_global_file
  1208. *
  1209. * Open a file in the top-level source directory.
  1210. */
  1211. static struct file *open_global_file( const struct makefile *make, const char *path, char **filename )
  1212. {
  1213. char *src_path = root_src_dir_path( path );
  1214. struct file *ret = load_file( src_path );
  1215. if (ret) *filename = src_path;
  1216. return ret;
  1217. }
  1218. /*******************************************************************
  1219. * open_global_header
  1220. *
  1221. * Open a file in the global include source directory.
  1222. */
  1223. static struct file *open_global_header( const struct makefile *make, const char *path, char **filename )
  1224. {
  1225. return open_global_file( make, strmake( "include/%s", path ), filename );
  1226. }
  1227. /*******************************************************************
  1228. * open_src_file
  1229. */
  1230. static struct file *open_src_file( const struct makefile *make, struct incl_file *pFile )
  1231. {
  1232. struct file *file = open_local_file( make, pFile->name, &pFile->filename );
  1233. if (!file) fatal_perror( "open %s", pFile->name );
  1234. return file;
  1235. }
  1236. /*******************************************************************
  1237. * open_include_file
  1238. */
  1239. static struct file *open_include_file( const struct makefile *make, struct incl_file *pFile )
  1240. {
  1241. struct file *file = NULL;
  1242. char *filename;
  1243. unsigned int i, len;
  1244. errno = ENOENT;
  1245. /* check for generated bison header */
  1246. if (strendswith( pFile->name, ".tab.h" ) &&
  1247. (file = open_local_file( make, replace_extension( pFile->name, ".tab.h", ".y" ), &filename )))
  1248. {
  1249. pFile->sourcename = filename;
  1250. pFile->filename = obj_dir_path( make, pFile->name );
  1251. return file;
  1252. }
  1253. /* check for corresponding idl file in source dir */
  1254. if (strendswith( pFile->name, ".h" ) &&
  1255. (file = open_local_file( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
  1256. {
  1257. pFile->sourcename = filename;
  1258. pFile->filename = obj_dir_path( make, pFile->name );
  1259. return file;
  1260. }
  1261. /* check for corresponding tlb file in source dir */
  1262. if (strendswith( pFile->name, ".tlb" ) &&
  1263. (file = open_local_file( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
  1264. {
  1265. pFile->sourcename = filename;
  1266. pFile->filename = obj_dir_path( make, pFile->name );
  1267. return file;
  1268. }
  1269. /* check for extra targets */
  1270. if (strarray_exists( &make->extra_targets, pFile->name ))
  1271. {
  1272. pFile->sourcename = src_dir_path( make, pFile->name );
  1273. pFile->filename = obj_dir_path( make, pFile->name );
  1274. return NULL;
  1275. }
  1276. /* now try in source dir */
  1277. if ((file = open_local_file( make, pFile->name, &pFile->filename ))) return file;
  1278. /* check for corresponding idl file in global includes */
  1279. if (strendswith( pFile->name, ".h" ) &&
  1280. (file = open_global_header( make, replace_extension( pFile->name, ".h", ".idl" ), &filename )))
  1281. {
  1282. pFile->sourcename = filename;
  1283. pFile->filename = strmake( "include/%s", pFile->name );
  1284. return file;
  1285. }
  1286. /* check for corresponding .in file in global includes (for config.h.in) */
  1287. if (strendswith( pFile->name, ".h" ) &&
  1288. (file = open_global_header( make, replace_extension( pFile->name, ".h", ".h.in" ), &filename )))
  1289. {
  1290. pFile->sourcename = filename;
  1291. pFile->filename = strmake( "include/%s", pFile->name );
  1292. return file;
  1293. }
  1294. /* check for corresponding .x file in global includes */
  1295. if (strendswith( pFile->name, "tmpl.h" ) &&
  1296. (file = open_global_header( make, replace_extension( pFile->name, ".h", ".x" ), &filename )))
  1297. {
  1298. pFile->sourcename = filename;
  1299. pFile->filename = strmake( "include/%s", pFile->name );
  1300. return file;
  1301. }
  1302. /* check for corresponding .tlb file in global includes */
  1303. if (strendswith( pFile->name, ".tlb" ) &&
  1304. (file = open_global_header( make, replace_extension( pFile->name, ".tlb", ".idl" ), &filename )))
  1305. {
  1306. pFile->sourcename = filename;
  1307. pFile->filename = strmake( "include/%s", pFile->name );
  1308. return file;
  1309. }
  1310. /* check in global includes source dir */
  1311. if ((file = open_global_header( make, pFile->name, &pFile->filename ))) return file;
  1312. /* check in global msvcrt includes */
  1313. if (pFile->use_msvcrt &&
  1314. (file = open_global_header( make, strmake( "msvcrt/%s", pFile->name ), &pFile->filename )))
  1315. return file;
  1316. /* now search in include paths */
  1317. for (i = 0; i < make->include_paths.count; i++)
  1318. {
  1319. const char *dir = make->include_paths.str[i];
  1320. if (root_src_dir)
  1321. {
  1322. len = strlen( root_src_dir );
  1323. if (!strncmp( dir, root_src_dir, len ) && (!dir[len] || dir[len] == '/'))
  1324. {
  1325. while (dir[len] == '/') len++;
  1326. file = open_global_file( make, concat_paths( dir + len, pFile->name ), &pFile->filename );
  1327. if (file) return file;
  1328. }
  1329. continue; /* ignore paths that don't point to the top source dir */
  1330. }
  1331. if (*dir != '/')
  1332. {
  1333. if ((file = open_include_path_file( make, dir, pFile->name, &pFile->filename )))
  1334. return file;
  1335. }
  1336. }
  1337. if (pFile->type == INCL_SYSTEM && pFile->use_msvcrt)
  1338. {
  1339. if (!strcmp( pFile->name, "stdarg.h" )) return NULL;
  1340. fprintf( stderr, "%s:%d: error: system header %s cannot be used with msvcrt\n",
  1341. pFile->included_by->file->name, pFile->included_line, pFile->name );
  1342. exit(1);
  1343. }
  1344. if (pFile->type == INCL_SYSTEM) return NULL; /* ignore system files we cannot find */
  1345. /* try in src file directory */
  1346. if ((file = open_file_same_dir( pFile->included_by, pFile->name, &pFile->filename ))) return file;
  1347. fprintf( stderr, "%s:%d: error: ", pFile->included_by->file->name, pFile->included_line );
  1348. perror( pFile->name );
  1349. pFile = pFile->included_by;
  1350. while (pFile && pFile->included_by)
  1351. {
  1352. const char *parent = pFile->included_by->sourcename;
  1353. if (!parent) parent = pFile->included_by->file->name;
  1354. fprintf( stderr, "%s:%d: note: %s was first included here\n",
  1355. parent, pFile->included_line, pFile->name );
  1356. pFile = pFile->included_by;
  1357. }
  1358. exit(1);
  1359. }
  1360. /*******************************************************************
  1361. * add_all_includes
  1362. */
  1363. static void add_all_includes( struct makefile *make, struct incl_file *parent, struct file *file )
  1364. {
  1365. unsigned int i;
  1366. parent->files_count = 0;
  1367. parent->files_size = file->deps_count;
  1368. parent->files = xmalloc( parent->files_size * sizeof(*parent->files) );
  1369. for (i = 0; i < file->deps_count; i++)
  1370. {
  1371. switch (file->deps[i].type)
  1372. {
  1373. case INCL_NORMAL:
  1374. case INCL_IMPORT:
  1375. add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
  1376. break;
  1377. case INCL_IMPORTLIB:
  1378. add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_IMPORTLIB );
  1379. break;
  1380. case INCL_SYSTEM:
  1381. add_include( make, parent, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
  1382. break;
  1383. case INCL_CPP_QUOTE:
  1384. case INCL_CPP_QUOTE_SYSTEM:
  1385. break;
  1386. }
  1387. }
  1388. }
  1389. /*******************************************************************
  1390. * parse_file
  1391. */
  1392. static void parse_file( struct makefile *make, struct incl_file *source, int src )
  1393. {
  1394. struct file *file = src ? open_src_file( make, source ) : open_include_file( make, source );
  1395. if (!file) return;
  1396. source->file = file;
  1397. source->files_count = 0;
  1398. source->files_size = file->deps_count;
  1399. source->files = xmalloc( source->files_size * sizeof(*source->files) );
  1400. if (file->flags & FLAG_C_UNIX) source->use_msvcrt = 0;
  1401. else if (file->flags & FLAG_C_IMPLIB) source->use_msvcrt = 1;
  1402. if (source->sourcename)
  1403. {
  1404. if (strendswith( source->sourcename, ".idl" ))
  1405. {
  1406. unsigned int i;
  1407. if (strendswith( source->name, ".tlb" )) return; /* typelibs don't include anything */
  1408. /* generated .h file always includes these */
  1409. add_include( make, source, "rpc.h", 0, INCL_NORMAL );
  1410. add_include( make, source, "rpcndr.h", 0, INCL_NORMAL );
  1411. for (i = 0; i < file->deps_count; i++)
  1412. {
  1413. switch (file->deps[i].type)
  1414. {
  1415. case INCL_IMPORT:
  1416. if (strendswith( file->deps[i].name, ".idl" ))
  1417. add_include( make, source, replace_extension( file->deps[i].name, ".idl", ".h" ),
  1418. file->deps[i].line, INCL_NORMAL );
  1419. else
  1420. add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
  1421. break;
  1422. case INCL_CPP_QUOTE:
  1423. add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_NORMAL );
  1424. break;
  1425. case INCL_CPP_QUOTE_SYSTEM:
  1426. add_include( make, source, file->deps[i].name, file->deps[i].line, INCL_SYSTEM );
  1427. break;
  1428. case INCL_NORMAL:
  1429. case INCL_SYSTEM:
  1430. case INCL_IMPORTLIB:
  1431. break;
  1432. }
  1433. }
  1434. return;
  1435. }
  1436. if (strendswith( source->sourcename, ".y" ))
  1437. return; /* generated .tab.h doesn't include anything */
  1438. }
  1439. add_all_includes( make, source, file );
  1440. }
  1441. /*******************************************************************
  1442. * add_src_file
  1443. *
  1444. * Add a source file to the list.
  1445. */
  1446. static struct incl_file *add_src_file( struct makefile *make, const char *name )
  1447. {
  1448. struct incl_file *file;
  1449. if ((file = find_src_file( make, name ))) return file; /* we already have it */
  1450. file = xmalloc( sizeof(*file) );
  1451. memset( file, 0, sizeof(*file) );
  1452. file->name = xstrdup(name);
  1453. file->use_msvcrt = make->use_msvcrt;
  1454. list_add_tail( &make->sources, &file->entry );
  1455. parse_file( make, file, 1 );
  1456. return file;
  1457. }
  1458. /*******************************************************************
  1459. * open_input_makefile
  1460. */
  1461. static FILE *open_input_makefile( const struct makefile *make )
  1462. {
  1463. FILE *ret;
  1464. if (make->obj_dir)
  1465. input_file_name = root_src_dir_path( obj_dir_path( make, strmake( "%s.in", output_makefile_name )));
  1466. else
  1467. input_file_name = output_makefile_name; /* always use output name for main Makefile */
  1468. input_line = 0;
  1469. if (!(ret = fopen( input_file_name, "r" ))) fatal_perror( "open" );
  1470. return ret;
  1471. }
  1472. /*******************************************************************
  1473. * get_make_variable
  1474. */
  1475. static const char *get_make_variable( const struct makefile *make, const char *name )
  1476. {
  1477. const char *ret;
  1478. if ((ret = strarray_get_value( &cmdline_vars, name ))) return ret;
  1479. if ((ret = strarray_get_value( &make->vars, name ))) return ret;
  1480. if (top_makefile && (ret = strarray_get_value( &top_makefile->vars, name ))) return ret;
  1481. return NULL;
  1482. }
  1483. /*******************************************************************
  1484. * get_expanded_make_variable
  1485. */
  1486. static char *get_expanded_make_variable( const struct makefile *make, const char *name )
  1487. {
  1488. const char *var;
  1489. char *p, *end, *expand, *tmp;
  1490. var = get_make_variable( make, name );
  1491. if (!var) return NULL;
  1492. p = expand = xstrdup( var );
  1493. while ((p = strchr( p, '$' )))
  1494. {
  1495. if (p[1] == '(')
  1496. {
  1497. if (!(end = strchr( p + 2, ')' ))) fatal_error( "syntax error in '%s'\n", expand );
  1498. *end++ = 0;
  1499. if (strchr( p + 2, ':' )) fatal_error( "pattern replacement not supported for '%s'\n", p + 2 );
  1500. var = get_make_variable( make, p + 2 );
  1501. tmp = replace_substr( expand, p, end - p, var ? var : "" );
  1502. /* switch to the new string */
  1503. p = tmp + (p - expand);
  1504. free( expand );
  1505. expand = tmp;
  1506. }
  1507. else if (p[1] == '{') /* don't expand ${} variables */
  1508. {
  1509. if (!(end = strchr( p + 2, '}' ))) fatal_error( "syntax error in '%s'\n", expand );
  1510. p = end + 1;
  1511. }
  1512. else if (p[1] == '$')
  1513. {
  1514. p += 2;
  1515. }
  1516. else fatal_error( "syntax error in '%s'\n", expand );
  1517. }
  1518. /* consider empty variables undefined */
  1519. p = expand;
  1520. while (*p && isspace(*p)) p++;
  1521. if (*p) return expand;
  1522. free( expand );
  1523. return NULL;
  1524. }
  1525. /*******************************************************************
  1526. * get_expanded_make_var_array
  1527. */
  1528. static struct strarray get_expanded_make_var_array( const struct makefile *make, const char *name )
  1529. {
  1530. struct strarray ret = empty_strarray;
  1531. char *value, *token;
  1532. if ((value = get_expanded_make_variable( make, name )))
  1533. for (token = strtok( value, " \t" ); token; token = strtok( NULL, " \t" ))
  1534. strarray_add( &ret, token );
  1535. return ret;
  1536. }
  1537. /*******************************************************************
  1538. * get_expanded_file_local_var
  1539. */
  1540. static struct strarray get_expanded_file_local_var( const struct makefile *make, const char *file,
  1541. const char *name )
  1542. {
  1543. char *p, *var = strmake( "%s_%s", file, name );
  1544. for (p = var; *p; p++) if (!isalnum( *p )) *p = '_';
  1545. return get_expanded_make_var_array( make, var );
  1546. }
  1547. /*******************************************************************
  1548. * set_make_variable
  1549. */
  1550. static int set_make_variable( struct strarray *array, const char *assignment )
  1551. {
  1552. char *p, *name;
  1553. p = name = xstrdup( assignment );
  1554. while (isalnum(*p) || *p == '_') p++;
  1555. if (name == p) return 0; /* not a variable */
  1556. if (isspace(*p))
  1557. {
  1558. *p++ = 0;
  1559. while (isspace(*p)) p++;
  1560. }
  1561. if (*p != '=') return 0; /* not an assignment */
  1562. *p++ = 0;
  1563. while (isspace(*p)) p++;
  1564. strarray_set_value( array, name, p );
  1565. return 1;
  1566. }
  1567. /*******************************************************************
  1568. * parse_makefile
  1569. */
  1570. static struct makefile *parse_makefile( const char *path )
  1571. {
  1572. char *buffer;
  1573. FILE *file;
  1574. struct makefile *make = xmalloc( sizeof(*make) );
  1575. memset( make, 0, sizeof(*make) );
  1576. make->obj_dir = path;
  1577. if (root_src_dir) make->src_dir = root_src_dir_path( make->obj_dir );
  1578. file = open_input_makefile( make );
  1579. while ((buffer = get_line( file )))
  1580. {
  1581. if (!strncmp( buffer, separator, strlen(separator) )) break;
  1582. if (*buffer == '\t') continue; /* command */
  1583. while (isspace( *buffer )) buffer++;
  1584. if (*buffer == '#') continue; /* comment */
  1585. set_make_variable( &make->vars, buffer );
  1586. }
  1587. fclose( file );
  1588. input_file_name = NULL;
  1589. return make;
  1590. }
  1591. /*******************************************************************
  1592. * add_generated_sources
  1593. */
  1594. static void add_generated_sources( struct makefile *make )
  1595. {
  1596. unsigned int i;
  1597. struct incl_file *source, *next, *file;
  1598. struct strarray objs = get_expanded_make_var_array( make, "EXTRA_OBJS" );
  1599. LIST_FOR_EACH_ENTRY_SAFE( source, next, &make->sources, struct incl_file, entry )
  1600. {
  1601. if (source->file->flags & FLAG_IDL_CLIENT)
  1602. {
  1603. file = add_generated_source( make, replace_extension( source->name, ".idl", "_c.c" ), NULL );
  1604. add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
  1605. add_all_includes( make, file, file->file );
  1606. }
  1607. if (source->file->flags & FLAG_IDL_SERVER)
  1608. {
  1609. file = add_generated_source( make, replace_extension( source->name, ".idl", "_s.c" ), NULL );
  1610. add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
  1611. add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
  1612. add_all_includes( make, file, file->file );
  1613. }
  1614. if (source->file->flags & FLAG_IDL_IDENT)
  1615. {
  1616. file = add_generated_source( make, replace_extension( source->name, ".idl", "_i.c" ), NULL );
  1617. add_dependency( file->file, "rpc.h", INCL_NORMAL );
  1618. add_dependency( file->file, "rpcndr.h", INCL_NORMAL );
  1619. add_dependency( file->file, "guiddef.h", INCL_NORMAL );
  1620. add_all_includes( make, file, file->file );
  1621. }
  1622. if (source->file->flags & FLAG_IDL_PROXY)
  1623. {
  1624. file = add_generated_source( make, "dlldata.o", "dlldata.c" );
  1625. add_dependency( file->file, "objbase.h", INCL_NORMAL );
  1626. add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
  1627. add_all_includes( make, file, file->file );
  1628. file = add_generated_source( make, replace_extension( source->name, ".idl", "_p.c" ), NULL );
  1629. add_dependency( file->file, "objbase.h", INCL_NORMAL );
  1630. add_dependency( file->file, "rpcproxy.h", INCL_NORMAL );
  1631. add_dependency( file->file, "wine/exception.h", INCL_NORMAL );
  1632. add_dependency( file->file, replace_extension( source->name, ".idl", ".h" ), INCL_NORMAL );
  1633. add_all_includes( make, file, file->file );
  1634. }
  1635. if (source->file->flags & FLAG_IDL_TYPELIB)
  1636. {
  1637. add_generated_source( make, replace_extension( source->name, ".idl", ".tlb" ), NULL );
  1638. }
  1639. if (source->file->flags & FLAG_IDL_REGTYPELIB)
  1640. {
  1641. add_generated_source( make, replace_extension( source->name, ".idl", "_t.res" ), NULL );
  1642. }
  1643. if (source->file->flags & FLAG_IDL_REGISTER)
  1644. {
  1645. add_generated_source( make, replace_extension( source->name, ".idl", "_r.res" ), NULL );
  1646. }
  1647. if (source->file->flags & FLAG_IDL_HEADER)
  1648. {
  1649. add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
  1650. }
  1651. if (!source->file->flags && strendswith( source->name, ".idl" ))
  1652. {
  1653. add_generated_source( make, replace_extension( source->name, ".idl", ".h" ), NULL );
  1654. }
  1655. if (strendswith( source->name, ".x" ))
  1656. {
  1657. add_generated_source( make, replace_extension( source->name, ".x", ".h" ), NULL );
  1658. }
  1659. if (strendswith( source->name, ".y" ))
  1660. {
  1661. file = add_generated_source( make, replace_extension( source->name, ".y", ".tab.c" ), NULL );
  1662. /* steal the includes list from the source file */
  1663. file->files_count = source->files_count;
  1664. file->files_size = source->files_size;
  1665. file->files = source->files;
  1666. source->files_count = source->files_size = 0;
  1667. source->files = NULL;
  1668. }
  1669. if (strendswith( source->name, ".l" ))
  1670. {
  1671. file = add_generated_source( make, replace_extension( source->name, ".l", ".yy.c" ), NULL );
  1672. /* steal the includes list from the source file */
  1673. file->files_count = source->files_count;
  1674. file->files_size = source->files_size;
  1675. file->files = source->files;
  1676. source->files_count = source->files_size = 0;
  1677. source->files = NULL;
  1678. }
  1679. if (source->file->flags & FLAG_C_IMPLIB)
  1680. {
  1681. if (!make->staticimplib && make->importlib && *dll_ext)
  1682. make->staticimplib = strmake( "lib%s.a", make->importlib );
  1683. }
  1684. if (strendswith( source->name, ".po" ))
  1685. {
  1686. if (!make->disabled)
  1687. strarray_add_uniq( &linguas, replace_extension( source->name, ".po", "" ));
  1688. }
  1689. if (strendswith( source->name, ".spec" ))
  1690. {
  1691. char *obj = replace_extension( source->name, ".spec", "" );
  1692. strarray_addall_uniq( &make->extra_imports,
  1693. get_expanded_file_local_var( make, obj, "IMPORTS" ));
  1694. }
  1695. }
  1696. if (make->testdll)
  1697. {
  1698. file = add_generated_source( make, "testlist.o", "testlist.c" );
  1699. add_dependency( file->file, "wine/test.h", INCL_NORMAL );
  1700. add_all_includes( make, file, file->file );
  1701. }
  1702. for (i = 0; i < objs.count; i++)
  1703. {
  1704. /* default to .c for unknown extra object files */
  1705. if (strendswith( objs.str[i], ".o" ))
  1706. {
  1707. file = add_generated_source( make, objs.str[i], replace_extension( objs.str[i], ".o", ".c" ));
  1708. if (make->module || make->staticlib) file->file->flags |= FLAG_C_UNIX;
  1709. }
  1710. else if (strendswith( objs.str[i], ".res" ))
  1711. add_generated_source( make, replace_extension( objs.str[i], ".res", ".rc" ), NULL );
  1712. else
  1713. add_generated_source( make, objs.str[i], NULL );
  1714. }
  1715. }
  1716. /*******************************************************************
  1717. * create_dir
  1718. */
  1719. static void create_dir( const char *dir )
  1720. {
  1721. char *p, *path;
  1722. p = path = xstrdup( dir );
  1723. while ((p = strchr( p, '/' )))
  1724. {
  1725. *p = 0;
  1726. if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
  1727. *p++ = '/';
  1728. while (*p == '/') p++;
  1729. }
  1730. if (mkdir( path, 0755 ) == -1 && errno != EEXIST) fatal_perror( "mkdir %s", path );
  1731. free( path );
  1732. }
  1733. /*******************************************************************
  1734. * create_file_directories
  1735. *
  1736. * Create the base directories of all the files.
  1737. */
  1738. static void create_file_directories( const struct makefile *make, struct strarray files )
  1739. {
  1740. struct strarray subdirs = empty_strarray;
  1741. unsigned int i;
  1742. char *dir;
  1743. for (i = 0; i < files.count; i++)
  1744. {
  1745. if (!strchr( files.str[i], '/' )) continue;
  1746. dir = obj_dir_path( make, files.str[i] );
  1747. *strrchr( dir, '/' ) = 0;
  1748. strarray_add_uniq( &subdirs, dir );
  1749. }
  1750. for (i = 0; i < subdirs.count; i++) create_dir( subdirs.str[i] );
  1751. }
  1752. /*******************************************************************
  1753. * output_filenames_obj_dir
  1754. */
  1755. static void output_filenames_obj_dir( const struct makefile *make, struct strarray array )
  1756. {
  1757. unsigned int i;
  1758. for (i = 0; i < array.count; i++) output_filename( obj_dir_path( make, array.str[i] ));
  1759. }
  1760. /*******************************************************************
  1761. * get_dependencies
  1762. */
  1763. static void get_dependencies( struct incl_file *file, struct incl_file *source )
  1764. {
  1765. unsigned int i;
  1766. if (!file->filename) return;
  1767. if (file != source)
  1768. {
  1769. if (file->owner == source) return; /* already processed */
  1770. if (file->type == INCL_IMPORTLIB &&
  1771. !(source->file->flags & (FLAG_IDL_TYPELIB | FLAG_IDL_REGTYPELIB)))
  1772. return; /* library is imported only when building a typelib */
  1773. file->owner = source;
  1774. strarray_add( &source->dependencies, file->filename );
  1775. }
  1776. for (i = 0; i < file->files_count; i++) get_dependencies( file->files[i], source );
  1777. }
  1778. /*******************************************************************
  1779. * get_local_dependencies
  1780. *
  1781. * Get the local dependencies of a given target.
  1782. */
  1783. static struct strarray get_local_dependencies( const struct makefile *make, const char *name,
  1784. struct strarray targets )
  1785. {
  1786. unsigned int i;
  1787. struct strarray deps = get_expanded_file_local_var( make, name, "DEPS" );
  1788. for (i = 0; i < deps.count; i++)
  1789. {
  1790. if (strarray_exists( &targets, deps.str[i] ))
  1791. deps.str[i] = obj_dir_path( make, deps.str[i] );
  1792. else
  1793. deps.str[i] = src_dir_path( make, deps.str[i] );
  1794. }
  1795. return deps;
  1796. }
  1797. /*******************************************************************
  1798. * get_static_lib
  1799. *
  1800. * Check if makefile builds the named static library and return the full lib path.
  1801. */
  1802. static const char *get_static_lib( const struct makefile *make, const char *name )
  1803. {
  1804. if (!make->staticlib) return NULL;
  1805. if (strncmp( make->staticlib, "lib", 3 )) return NULL;
  1806. if (strncmp( make->staticlib + 3, name, strlen(name) )) return NULL;
  1807. if (strcmp( make->staticlib + 3 + strlen(name), ".a" )) return NULL;
  1808. return obj_dir_path( make, make->staticlib );
  1809. }
  1810. /*******************************************************************
  1811. * get_parent_makefile
  1812. */
  1813. static struct makefile *get_parent_makefile( struct makefile *make )
  1814. {
  1815. char *dir, *p;
  1816. int i;
  1817. if (!make->obj_dir) return NULL;
  1818. dir = xstrdup( make->obj_dir );
  1819. if (!(p = strrchr( dir, '/' ))) return NULL;
  1820. *p = 0;
  1821. for (i = 0; i < subdirs.count; i++)
  1822. if (!strcmp( submakes[i]->obj_dir, dir )) return submakes[i];
  1823. return NULL;
  1824. }
  1825. /*******************************************************************
  1826. * needs_cross_lib
  1827. */
  1828. static int needs_cross_lib( const struct makefile *make )
  1829. {
  1830. const char *name;
  1831. if (!crosstarget) return 0;
  1832. if (make->importlib) name = make->importlib;
  1833. else if (make->staticlib)
  1834. {
  1835. name = replace_extension( make->staticlib, ".a", "" );
  1836. if (!strncmp( name, "lib", 3 )) name += 3;
  1837. }
  1838. else return 0;
  1839. if (strarray_exists( &cross_import_libs, name )) return 1;
  1840. if (delay_load_flag && strarray_exists( &delay_import_libs, name )) return 1;
  1841. return 0;
  1842. }
  1843. /*******************************************************************
  1844. * needs_delay_lib
  1845. */
  1846. static int needs_delay_lib( const struct makefile *make )
  1847. {
  1848. if (delay_load_flag) return 0;
  1849. if (*dll_ext && !crosstarget) return 0;
  1850. if (!make->importlib) return 0;
  1851. return strarray_exists( &delay_import_libs, make->importlib );
  1852. }
  1853. /*******************************************************************
  1854. * needs_implib_symlink
  1855. */
  1856. static int needs_implib_symlink( const struct makefile *make )
  1857. {
  1858. if (!make->module) return 0;
  1859. if (!make->importlib) return 0;
  1860. if (make->is_win16 && make->disabled) return 0;
  1861. if (strncmp( make->obj_dir, "dlls/", 5 )) return 0;
  1862. if (!strcmp( make->module, make->importlib )) return 0;
  1863. if (!strchr( make->importlib, '.' ) &&
  1864. !strncmp( make->module, make->importlib, strlen( make->importlib )) &&
  1865. !strcmp( make->module + strlen( make->importlib ), ".dll" ))
  1866. return 0;
  1867. return 1;
  1868. }
  1869. /*******************************************************************
  1870. * add_unix_libraries
  1871. */
  1872. static struct strarray add_unix_libraries( const struct makefile *make, struct strarray *deps )
  1873. {
  1874. struct strarray ret = empty_strarray;
  1875. struct strarray all_libs = empty_strarray;
  1876. unsigned int i, j;
  1877. strarray_add( &all_libs, "-lwine_port" );
  1878. strarray_addall( &all_libs, get_expanded_make_var_array( make, "EXTRALIBS" ));
  1879. strarray_addall( &all_libs, libs );
  1880. for (i = 0; i < all_libs.count; i++)
  1881. {
  1882. const char *lib = NULL;
  1883. if (!strncmp( all_libs.str[i], "-l", 2 ))
  1884. {
  1885. const char *name = all_libs.str[i] + 2;
  1886. for (j = 0; j < subdirs.count; j++)
  1887. if ((lib = get_static_lib( submakes[j], name ))) break;
  1888. }
  1889. if (lib)
  1890. {
  1891. strarray_add( deps, lib );
  1892. strarray_add( &ret, lib );
  1893. }
  1894. else strarray_add( &ret, all_libs.str[i] );
  1895. }
  1896. return ret;
  1897. }
  1898. /*******************************************************************
  1899. * add_import_libs
  1900. */
  1901. static struct strarray add_import_libs( const struct makefile *make, struct strarray *deps,
  1902. struct strarray imports, int delay, int is_unix )
  1903. {
  1904. struct strarray ret = empty_strarray;
  1905. unsigned int i, j;
  1906. int is_cross = make->is_cross && !is_unix;
  1907. for (i = 0; i < imports.count; i++)
  1908. {
  1909. const char *name = get_base_name( imports.str[i] );
  1910. const char *lib = NULL;
  1911. /* skip module's own importlib, its object files will be linked directly */
  1912. if (make->importlib && !strcmp( make->importlib, imports.str[i] )) continue;
  1913. for (j = 0; j < subdirs.count; j++)
  1914. {
  1915. if (submakes[j]->importlib && !strcmp( submakes[j]->importlib, name ))
  1916. {
  1917. if (is_cross || !*dll_ext || submakes[j]->staticimplib)
  1918. lib = obj_dir_path( submakes[j], strmake( "lib%s.a", name ));
  1919. else
  1920. {
  1921. strarray_add( deps, strmake( "%s/lib%s.def", submakes[j]->obj_dir, name ));
  1922. if (needs_implib_symlink( submakes[j] ))
  1923. strarray_add( deps, strmake( "dlls/lib%s.def", name ));
  1924. }
  1925. break;
  1926. }
  1927. if ((lib = get_static_lib( submakes[j], name ))) break;
  1928. }
  1929. if (lib)
  1930. {
  1931. const char *ext = NULL;
  1932. if (delay && !delay_load_flag && (is_cross || !*dll_ext)) ext = ".delay.a";
  1933. else if (is_cross) ext = ".cross.a";
  1934. if (ext) lib = replace_extension( lib, ".a", ext );
  1935. strarray_add( deps, lib );
  1936. strarray_add( &ret, lib );
  1937. if (needs_implib_symlink( submakes[j] ))
  1938. strarray_add( deps, strmake( "dlls/lib%s%s", name, ext ? ext : ".a" ));
  1939. }
  1940. else strarray_add( &ret, strmake( "-l%s", name ));
  1941. }
  1942. return ret;
  1943. }
  1944. /*******************************************************************
  1945. * get_default_imports
  1946. */
  1947. static struct strarray get_default_imports( const struct makefile *make )
  1948. {
  1949. struct strarray ret = empty_strarray;
  1950. if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) return ret;
  1951. strarray_add( &ret, "winecrt0" );
  1952. if (make->is_win16) strarray_add( &ret, "kernel" );
  1953. strarray_add( &ret, "kernel32" );
  1954. strarray_add( &ret, "ntdll" );
  1955. return ret;
  1956. }
  1957. /*******************************************************************
  1958. * add_crt_import
  1959. */
  1960. static const char *add_crt_import( const struct makefile *make, struct strarray *imports )
  1961. {
  1962. unsigned int i;
  1963. const char *crt_dll = NULL;
  1964. for (i = 0; i < imports->count; i++)
  1965. {
  1966. if (strncmp( imports->str[i], "msvcr", 5 ) && strncmp( imports->str[i], "ucrt", 4 )) continue;
  1967. if (crt_dll) fatal_error( "More than one C runtime DLL imported: %s and %s\n", crt_dll, imports->str[i] );
  1968. crt_dll = imports->str[i];
  1969. }
  1970. if (!crt_dll && !strarray_exists( &make->extradllflags, "-nodefaultlibs" ))
  1971. {
  1972. crt_dll = !make->testdll && !make->staticlib ? "ucrtbase" : "msvcrt";
  1973. strarray_add( imports, crt_dll );
  1974. }
  1975. return crt_dll;
  1976. }
  1977. /*******************************************************************
  1978. * add_install_rule
  1979. */
  1980. static void add_install_rule( struct makefile *make, const char *target,
  1981. const char *file, const char *dest )
  1982. {
  1983. if (strarray_exists( &make->install_lib, target ) ||
  1984. strarray_exists( &top_install_lib, make->obj_dir ) ||
  1985. strarray_exists( &top_install_lib, obj_dir_path( make, target )))
  1986. {
  1987. strarray_add( &make->install_rules[INSTALL_LIB], file );
  1988. strarray_add( &make->install_rules[INSTALL_LIB], dest );
  1989. }
  1990. else if (strarray_exists( &make->install_dev, target ) ||
  1991. strarray_exists( &top_install_dev, make->obj_dir ) ||
  1992. strarray_exists( &top_install_dev, obj_dir_path( make, target )))
  1993. {
  1994. strarray_add( &make->install_rules[INSTALL_DEV], file );
  1995. strarray_add( &make->install_rules[INSTALL_DEV], dest );
  1996. }
  1997. }
  1998. /*******************************************************************
  1999. * get_include_install_path
  2000. *
  2001. * Determine the installation path for a given include file.
  2002. */
  2003. static const char *get_include_install_path( const char *name )
  2004. {
  2005. if (!strncmp( name, "wine/", 5 )) return name + 5;
  2006. if (!strncmp( name, "msvcrt/", 7 )) return name;
  2007. return strmake( "windows/%s", name );
  2008. }
  2009. /*******************************************************************
  2010. * get_shared_library_name
  2011. *
  2012. * Determine possible names for a shared library with a version number.
  2013. */
  2014. static struct strarray get_shared_lib_names( const char *libname )
  2015. {
  2016. struct strarray ret = empty_strarray;
  2017. const char *ext, *p;
  2018. char *name, *first, *second;
  2019. size_t len = 0;
  2020. strarray_add( &ret, libname );
  2021. for (p = libname; (p = strchr( p, '.' )); p++)
  2022. if ((len = strspn( p + 1, "0123456789." ))) break;
  2023. if (!len) return ret;
  2024. ext = p + 1 + len;
  2025. if (*ext && ext[-1] == '.') ext--;
  2026. /* keep only the first group of digits */
  2027. name = xstrdup( libname );
  2028. first = name + (p - libname);
  2029. if ((second = strchr( first + 1, '.' )))
  2030. {
  2031. strcpy( second, ext );
  2032. strarray_add( &ret, xstrdup( name ));
  2033. }
  2034. /* now remove all digits */
  2035. strcpy( first, ext );
  2036. strarray_add( &ret, name );
  2037. return ret;
  2038. }
  2039. /*******************************************************************
  2040. * get_source_defines
  2041. */
  2042. static struct strarray get_source_defines( struct makefile *make, struct incl_file *source,
  2043. const char *obj )
  2044. {
  2045. unsigned int i;
  2046. struct strarray ret = empty_strarray;
  2047. strarray_addall( &ret, make->include_args );
  2048. if (source->use_msvcrt)
  2049. strarray_add( &ret, strmake( "-I%s", root_src_dir_path( "include/msvcrt" )));
  2050. for (i = 0; i < make->include_paths.count; i++)
  2051. strarray_add( &ret, strmake( "-I%s", make->include_paths.str[i] ));
  2052. strarray_addall( &ret, make->define_args );
  2053. strarray_addall( &ret, get_expanded_file_local_var( make, obj, "EXTRADEFS" ));
  2054. if ((source->file->flags & FLAG_C_UNIX) && *dll_ext) strarray_add( &ret, "-DWINE_UNIX_LIB" );
  2055. return ret;
  2056. }
  2057. /*******************************************************************
  2058. * get_debug_file
  2059. */
  2060. static const char *get_debug_file( struct makefile *make, const char *name )
  2061. {
  2062. const char *debug_file = NULL;
  2063. if (!make->is_cross || !crossdebug) return NULL;
  2064. if (!strcmp( crossdebug, "pdb" )) debug_file = strmake( "%s.pdb", get_base_name( name ));
  2065. else if(!strncmp( crossdebug, "split", 5 )) debug_file = strmake( "%s.debug", name );
  2066. if (debug_file) strarray_add( &make->debug_files, debug_file );
  2067. return debug_file;
  2068. }
  2069. /*******************************************************************
  2070. * output_winegcc_command
  2071. */
  2072. static void output_winegcc_command( struct makefile *make, int is_cross )
  2073. {
  2074. output( "\t%s -o $@", tools_path( make, "winegcc" ));
  2075. output_filename( "--wine-objdir ." );
  2076. if (tools_dir)
  2077. {
  2078. output_filename( "--winebuild" );
  2079. output_filename( tools_path( make, "winebuild" ));
  2080. }
  2081. if (is_cross)
  2082. {
  2083. output_filename( "-b" );
  2084. output_filename( crosstarget );
  2085. output_filename( "--lib-suffix=.cross.a" );
  2086. }
  2087. else
  2088. {
  2089. output_filenames( target_flags );
  2090. output_filenames( lddll_flags );
  2091. }
  2092. }
  2093. /*******************************************************************
  2094. * output_symlink_rule
  2095. *
  2096. * Output a rule to create a symlink.
  2097. */
  2098. static void output_symlink_rule( const char *src_name, const char *link_name )
  2099. {
  2100. const char *name;
  2101. output( "\trm -f %s && ", link_name );
  2102. /* dest path with a directory needs special handling if ln -s isn't supported */
  2103. if (strcmp( ln_s, "ln -s" ) && ((name = strrchr( link_name, '/' ))))
  2104. {
  2105. char *dir = xstrdup( link_name );
  2106. dir[name - link_name] = 0;
  2107. output( "cd %s && %s %s %s\n", *dir ? dir : "/", ln_s, src_name, name + 1 );
  2108. free( dir );
  2109. }
  2110. else
  2111. {
  2112. output( "%s %s %s\n", ln_s, src_name, link_name );
  2113. }
  2114. }
  2115. /*******************************************************************
  2116. * output_srcdir_symlink
  2117. *
  2118. * Output rule to create a symlink back to the source directory, for source files
  2119. * that are needed at run-time.
  2120. */
  2121. static void output_srcdir_symlink( struct makefile *make, const char *obj )
  2122. {
  2123. char *src_file, *dst_file, *src_name;
  2124. if (!make->src_dir) return;
  2125. src_file = src_dir_path( make, obj );
  2126. dst_file = obj_dir_path( make, obj );
  2127. output( "%s: %s\n", dst_file, src_file );
  2128. src_name = src_file;
  2129. if (src_name[0] != '/' && make->obj_dir)
  2130. src_name = concat_paths( get_relative_path( make->obj_dir, "" ), src_name );
  2131. output_symlink_rule( src_name, dst_file );
  2132. strarray_add( &make->all_targets, obj );
  2133. }
  2134. /*******************************************************************
  2135. * output_install_commands
  2136. */
  2137. static void output_install_commands( struct makefile *make, struct strarray files )
  2138. {
  2139. unsigned int i;
  2140. char *install_sh = root_src_dir_path( "tools/install-sh" );
  2141. for (i = 0; i < files.count; i += 2)
  2142. {
  2143. const char *file = files.str[i];
  2144. const char *dest = strmake( "$(DESTDIR)%s", files.str[i + 1] + 1 );
  2145. switch (*files.str[i + 1])
  2146. {
  2147. case 'c': /* cross-compiled program */
  2148. output( "\tSTRIPPROG=%s-strip %s -m 644 $(INSTALL_PROGRAM_FLAGS) %s %s\n",
  2149. crosstarget, install_sh, obj_dir_path( make, file ), dest );
  2150. output( "\t%s --builtin %s\n", tools_path( make, "winebuild" ), dest );
  2151. break;
  2152. case 'd': /* data file */
  2153. output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
  2154. install_sh, obj_dir_path( make, file ), dest );
  2155. break;
  2156. case 'D': /* data file in source dir */
  2157. output( "\t%s -m 644 $(INSTALL_DATA_FLAGS) %s %s\n",
  2158. install_sh, src_dir_path( make, file ), dest );
  2159. break;
  2160. case 'p': /* program file */
  2161. output( "\tSTRIPPROG=\"$(STRIP)\" %s $(INSTALL_PROGRAM_FLAGS) %s %s\n",
  2162. install_sh, obj_dir_path( make, file ), dest );
  2163. break;
  2164. case 's': /* script */
  2165. output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
  2166. install_sh, obj_dir_path( make, file ), dest );
  2167. break;
  2168. case 'S': /* script in source dir */
  2169. output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
  2170. install_sh, src_dir_path( make, file ), dest );
  2171. break;
  2172. case 't': /* script in tools dir */
  2173. output( "\t%s $(INSTALL_SCRIPT_FLAGS) %s %s\n",
  2174. install_sh, tools_dir_path( make, files.str[i] ), dest );
  2175. break;
  2176. case 'y': /* symlink */
  2177. output_symlink_rule( files.str[i], dest );
  2178. break;
  2179. default:
  2180. assert(0);
  2181. }
  2182. strarray_add( &make->uninstall_files, dest );
  2183. }
  2184. }
  2185. /*******************************************************************
  2186. * output_install_rules
  2187. *
  2188. * Rules are stored as a (file,dest) pair of values.
  2189. * The first char of dest indicates the type of install.
  2190. */
  2191. static void output_install_rules( struct makefile *make, enum install_rules rules, const char *target )
  2192. {
  2193. unsigned int i;
  2194. struct strarray files = make->install_rules[rules];
  2195. struct strarray targets = empty_strarray;
  2196. if (!files.count) return;
  2197. for (i = 0; i < files.count; i += 2)
  2198. {
  2199. const char *file = files.str[i];
  2200. switch (*files.str[i + 1])
  2201. {
  2202. case 'c': /* cross-compiled program */
  2203. case 'd': /* data file */
  2204. case 'p': /* program file */
  2205. case 's': /* script */
  2206. strarray_add_uniq( &targets, obj_dir_path( make, file ));
  2207. break;
  2208. case 't': /* script in tools dir */
  2209. strarray_add_uniq( &targets, tools_dir_path( make, file ));
  2210. break;
  2211. }
  2212. }
  2213. output( "%s %s::", obj_dir_path( make, "install" ), obj_dir_path( make, target ));
  2214. output_filenames( targets );
  2215. output( "\n" );
  2216. output_install_commands( make, files );
  2217. strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "install" ));
  2218. strarray_add_uniq( &make->phony_targets, obj_dir_path( make, target ));
  2219. }
  2220. static int cmp_string_length( const char **a, const char **b )
  2221. {
  2222. int paths_a = 0, paths_b = 0;
  2223. const char *p;
  2224. for (p = *a; *p; p++) if (*p == '/') paths_a++;
  2225. for (p = *b; *p; p++) if (*p == '/') paths_b++;
  2226. if (paths_b != paths_a) return paths_b - paths_a;
  2227. return strcmp( *a, *b );
  2228. }
  2229. /*******************************************************************
  2230. * output_uninstall_rules
  2231. */
  2232. static void output_uninstall_rules( struct makefile *make )
  2233. {
  2234. static const char *dirs_order[] =
  2235. { "$(includedir)", "$(mandir)", "$(fontdir)", "$(datadir)", "$(dlldir)" };
  2236. struct strarray uninstall_dirs = empty_strarray;
  2237. unsigned int i, j;
  2238. if (!make->uninstall_files.count) return;
  2239. output( "uninstall::\n" );
  2240. output_rm_filenames( make->uninstall_files );
  2241. strarray_add_uniq( &make->phony_targets, "uninstall" );
  2242. if (!subdirs.count) return;
  2243. for (i = 0; i < make->uninstall_files.count; i++)
  2244. {
  2245. char *dir = xstrdup( make->uninstall_files.str[i] );
  2246. while (strchr( dir, '/' ))
  2247. {
  2248. *strrchr( dir, '/' ) = 0;
  2249. strarray_add_uniq( &uninstall_dirs, xstrdup(dir) );
  2250. }
  2251. }
  2252. strarray_qsort( &uninstall_dirs, cmp_string_length );
  2253. output( "\t-rmdir" );
  2254. for (i = 0; i < sizeof(dirs_order)/sizeof(dirs_order[0]); i++)
  2255. {
  2256. for (j = 0; j < uninstall_dirs.count; j++)
  2257. {
  2258. if (!uninstall_dirs.str[j]) continue;
  2259. if (strncmp( uninstall_dirs.str[j] + strlen("$(DESTDIR)"), dirs_order[i], strlen(dirs_order[i]) ))
  2260. continue;
  2261. output_filename( uninstall_dirs.str[j] );
  2262. uninstall_dirs.str[j] = NULL;
  2263. }
  2264. }
  2265. for (j = 0; j < uninstall_dirs.count; j++)
  2266. if (uninstall_dirs.str[j]) output_filename( uninstall_dirs.str[j] );
  2267. output( "\n" );
  2268. }
  2269. /*******************************************************************
  2270. * output_importlib_symlinks
  2271. */
  2272. static struct strarray output_importlib_symlinks( const struct makefile *make )
  2273. {
  2274. struct strarray ret = empty_strarray;
  2275. const char *lib, *dst, *ext[4];
  2276. int i, count = 0;
  2277. ext[count++] = (*dll_ext && !make->implib_objs.count) ? "def" : "a";
  2278. if (needs_delay_lib( make )) ext[count++] = "delay.a";
  2279. if (needs_cross_lib( make )) ext[count++] = "cross.a";
  2280. for (i = 0; i < count; i++)
  2281. {
  2282. lib = strmake( "lib%s.%s", make->importlib, ext[i] );
  2283. dst = strmake( "dlls/%s", lib );
  2284. output( "%s: %s\n", dst, obj_dir_path( make, lib ));
  2285. output_symlink_rule( concat_paths( make->obj_dir + strlen("dlls/"), lib ), dst );
  2286. strarray_add( &ret, dst );
  2287. }
  2288. return ret;
  2289. }
  2290. /*******************************************************************
  2291. * output_po_files
  2292. */
  2293. static void output_po_files( const struct makefile *make )
  2294. {
  2295. const char *po_dir = src_dir_path( make, "po" );
  2296. unsigned int i;
  2297. if (linguas.count)
  2298. {
  2299. for (i = 0; i < linguas.count; i++)
  2300. output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
  2301. output( ": %s/wine.pot\n", po_dir );
  2302. output( "\tmsgmerge --previous -q $@ %s/wine.pot | msgattrib --no-obsolete -o $@.new && mv $@.new $@\n",
  2303. po_dir );
  2304. output( "po:" );
  2305. for (i = 0; i < linguas.count; i++)
  2306. output_filename( strmake( "%s/%s.po", po_dir, linguas.str[i] ));
  2307. output( "\n" );
  2308. }
  2309. output( "%s/wine.pot:", po_dir );
  2310. output_filenames( make->pot_files );
  2311. output( "\n" );
  2312. output( "\tmsgcat -o $@" );
  2313. output_filenames( make->pot_files );
  2314. output( "\n" );
  2315. }
  2316. /*******************************************************************
  2317. * output_source_y
  2318. */
  2319. static void output_source_y( struct makefile *make, struct incl_file *source, const char *obj )
  2320. {
  2321. /* add source file dependency for parallel makes */
  2322. char *header = strmake( "%s.tab.h", obj );
  2323. if (find_include_file( make, header ))
  2324. {
  2325. output( "%s: %s\n", obj_dir_path( make, header ), source->filename );
  2326. output( "\t%s -p %s_ -o %s.tab.c -d %s\n",
  2327. bison, obj, obj_dir_path( make, obj ), source->filename );
  2328. output( "%s.tab.c: %s %s\n", obj_dir_path( make, obj ),
  2329. source->filename, obj_dir_path( make, header ));
  2330. strarray_add( &make->clean_files, header );
  2331. }
  2332. else output( "%s.tab.c: %s\n", obj_dir_path( make, obj ), source->filename );
  2333. output( "\t%s -p %s_ -o $@ %s\n", bison, obj, source->filename );
  2334. }
  2335. /*******************************************************************
  2336. * output_source_l
  2337. */
  2338. static void output_source_l( struct makefile *make, struct incl_file *source, const char *obj )
  2339. {
  2340. output( "%s.yy.c: %s\n", obj_dir_path( make, obj ), source->filename );
  2341. output( "\t%s -o$@ %s\n", flex, source->filename );
  2342. }
  2343. /*******************************************************************
  2344. * output_source_h
  2345. */
  2346. static void output_source_h( struct makefile *make, struct incl_file *source, const char *obj )
  2347. {
  2348. if (source->file->flags & FLAG_GENERATED)
  2349. strarray_add( &make->all_targets, source->name );
  2350. else
  2351. add_install_rule( make, source->name, source->name,
  2352. strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
  2353. }
  2354. /*******************************************************************
  2355. * output_source_rc
  2356. */
  2357. static void output_source_rc( struct makefile *make, struct incl_file *source, const char *obj )
  2358. {
  2359. struct strarray defines = get_source_defines( make, source, obj );
  2360. const char *po_dir = NULL;
  2361. unsigned int i;
  2362. if (source->file->flags & FLAG_GENERATED) strarray_add( &make->clean_files, source->name );
  2363. if (linguas.count && (source->file->flags & FLAG_RC_PO)) po_dir = "po";
  2364. strarray_add( &make->res_files, strmake( "%s.res", obj ));
  2365. if (source->file->flags & FLAG_RC_PO)
  2366. {
  2367. strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
  2368. output( "%s.pot ", obj_dir_path( make, obj ) );
  2369. }
  2370. output( "%s.res: %s", obj_dir_path( make, obj ), source->filename );
  2371. output_filename( tools_path( make, "wrc" ));
  2372. output_filenames( source->dependencies );
  2373. output( "\n" );
  2374. output( "\t%s -u -o $@", tools_path( make, "wrc" ) );
  2375. if (make->is_win16) output_filename( "-m16" );
  2376. else output_filenames( target_flags );
  2377. output_filename( "--nostdinc" );
  2378. if (po_dir) output_filename( strmake( "--po-dir=%s", po_dir ));
  2379. output_filenames( defines );
  2380. output_filename( source->filename );
  2381. output( "\n" );
  2382. if (po_dir)
  2383. {
  2384. output( "%s.res:", obj_dir_path( make, obj ));
  2385. for (i = 0; i < linguas.count; i++)
  2386. output_filename( strmake( "%s/%s.mo", po_dir, linguas.str[i] ));
  2387. output( "\n" );
  2388. }
  2389. }
  2390. /*******************************************************************
  2391. * output_source_mc
  2392. */
  2393. static void output_source_mc( struct makefile *make, struct incl_file *source, const char *obj )
  2394. {
  2395. unsigned int i;
  2396. char *obj_path = obj_dir_path( make, obj );
  2397. strarray_add( &make->res_files, strmake( "%s.res", obj ));
  2398. strarray_add( &make->pot_files, strmake( "%s.pot", obj ));
  2399. output( "%s.pot %s.res: %s", obj_path, obj_path, source->filename );
  2400. output_filename( tools_path( make, "wmc" ));
  2401. output_filenames( source->dependencies );
  2402. output( "\n" );
  2403. output( "\t%s -u -o $@ %s", tools_path( make, "wmc" ), source->filename );
  2404. if (linguas.count)
  2405. {
  2406. output_filename( "--po-dir=po" );
  2407. output( "\n" );
  2408. output( "%s.res:", obj_dir_path( make, obj ));
  2409. for (i = 0; i < linguas.count; i++)
  2410. output_filename( strmake( "po/%s.mo", linguas.str[i] ));
  2411. }
  2412. output( "\n" );
  2413. }
  2414. /*******************************************************************
  2415. * output_source_res
  2416. */
  2417. static void output_source_res( struct makefile *make, struct incl_file *source, const char *obj )
  2418. {
  2419. strarray_add( &make->res_files, source->name );
  2420. }
  2421. /*******************************************************************
  2422. * output_source_idl
  2423. */
  2424. static void output_source_idl( struct makefile *make, struct incl_file *source, const char *obj )
  2425. {
  2426. struct strarray defines = get_source_defines( make, source, obj );
  2427. struct strarray targets = empty_strarray;
  2428. char *dest;
  2429. unsigned int i;
  2430. if (!source->file->flags) source->file->flags |= FLAG_IDL_HEADER | FLAG_INSTALL;
  2431. if (find_include_file( make, strmake( "%s.h", obj ))) source->file->flags |= FLAG_IDL_HEADER;
  2432. for (i = 0; i < sizeof(idl_outputs) / sizeof(idl_outputs[0]); i++)
  2433. {
  2434. if (!(source->file->flags & idl_outputs[i].flag)) continue;
  2435. dest = strmake( "%s%s", obj, idl_outputs[i].ext );
  2436. if (!find_src_file( make, dest )) strarray_add( &make->clean_files, dest );
  2437. strarray_add( &targets, dest );
  2438. }
  2439. if (source->file->flags & FLAG_IDL_PROXY) strarray_add( &make->dlldata_files, source->name );
  2440. if (source->file->flags & FLAG_INSTALL)
  2441. {
  2442. add_install_rule( make, source->name, xstrdup( source->name ),
  2443. strmake( "D$(includedir)/wine/%s.idl", get_include_install_path( obj ) ));
  2444. if (source->file->flags & FLAG_IDL_HEADER)
  2445. add_install_rule( make, source->name, strmake( "%s.h", obj ),
  2446. strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
  2447. if (source->file->flags & FLAG_IDL_TYPELIB)
  2448. add_install_rule( make, source->name, strmake( "%s.tlb", obj ),
  2449. strmake( "d$(includedir)/wine/%s.tlb", get_include_install_path( obj ) ));
  2450. }
  2451. if (!targets.count) return;
  2452. output_filenames_obj_dir( make, targets );
  2453. output( ": %s\n", tools_path( make, "widl" ));
  2454. output( "\t%s -o $@", tools_path( make, "widl" ) );
  2455. output_filenames( target_flags );
  2456. output_filename( "--nostdinc" );
  2457. output_filenames( defines );
  2458. output_filenames( get_expanded_make_var_array( make, "EXTRAIDLFLAGS" ));
  2459. output_filenames( get_expanded_file_local_var( make, obj, "EXTRAIDLFLAGS" ));
  2460. output_filename( source->filename );
  2461. output( "\n" );
  2462. output_filenames_obj_dir( make, targets );
  2463. output( ": %s", source->filename );
  2464. output_filenames( source->dependencies );
  2465. output( "\n" );
  2466. }
  2467. /*******************************************************************
  2468. * output_source_tlb
  2469. */
  2470. static void output_source_tlb( struct makefile *make, struct incl_file *source, const char *obj )
  2471. {
  2472. strarray_add( &make->all_targets, source->name );
  2473. }
  2474. /*******************************************************************
  2475. * output_source_x
  2476. */
  2477. static void output_source_x( struct makefile *make, struct incl_file *source, const char *obj )
  2478. {
  2479. output( "%s.h: %s%s %s\n", obj_dir_path( make, obj ),
  2480. tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
  2481. output( "\t%s%s -H -o $@ %s\n",
  2482. tools_dir_path( make, "make_xftmpl" ), tools_ext, source->filename );
  2483. if (source->file->flags & FLAG_INSTALL)
  2484. {
  2485. add_install_rule( make, source->name, source->name,
  2486. strmake( "D$(includedir)/wine/%s", get_include_install_path( source->name ) ));
  2487. add_install_rule( make, source->name, strmake( "%s.h", obj ),
  2488. strmake( "d$(includedir)/wine/%s.h", get_include_install_path( obj ) ));
  2489. }
  2490. }
  2491. /*******************************************************************
  2492. * output_source_sfd
  2493. */
  2494. static void output_source_sfd( struct makefile *make, struct incl_file *source, const char *obj )
  2495. {
  2496. unsigned int i;
  2497. char *ttf_obj = strmake( "%s.ttf", obj );
  2498. char *ttf_file = src_dir_path( make, ttf_obj );
  2499. if (fontforge && !make->src_dir)
  2500. {
  2501. output( "%s: %s\n", ttf_file, source->filename );
  2502. output( "\t%s -script %s %s $@\n",
  2503. fontforge, root_src_dir_path( "fonts/genttf.ff" ), source->filename );
  2504. if (!(source->file->flags & FLAG_SFD_FONTS)) strarray_add( &make->font_files, ttf_obj );
  2505. }
  2506. if (source->file->flags & FLAG_INSTALL)
  2507. {
  2508. add_install_rule( make, source->name, ttf_obj, strmake( "D$(fontdir)/%s", ttf_obj ));
  2509. output_srcdir_symlink( make, ttf_obj );
  2510. }
  2511. if (source->file->flags & FLAG_SFD_FONTS)
  2512. {
  2513. struct strarray *array = source->file->args;
  2514. for (i = 0; i < array->count; i++)
  2515. {
  2516. char *font = strtok( xstrdup(array->str[i]), " \t" );
  2517. char *args = strtok( NULL, "" );
  2518. strarray_add( &make->all_targets, xstrdup( font ));
  2519. output( "%s: %s %s\n", obj_dir_path( make, font ),
  2520. tools_path( make, "sfnt2fon" ), ttf_file );
  2521. output( "\t%s -q -o $@ %s %s\n", tools_path( make, "sfnt2fon" ), ttf_file, args );
  2522. add_install_rule( make, source->name, xstrdup(font), strmake( "d$(fontdir)/%s", font ));
  2523. }
  2524. }
  2525. }
  2526. /*******************************************************************
  2527. * output_source_svg
  2528. */
  2529. static void output_source_svg( struct makefile *make, struct incl_file *source, const char *obj )
  2530. {
  2531. static const char * const images[] = { "bmp", "cur", "ico", NULL };
  2532. unsigned int i;
  2533. if (convert && rsvg && icotool && !make->src_dir)
  2534. {
  2535. for (i = 0; images[i]; i++)
  2536. if (find_include_file( make, strmake( "%s.%s", obj, images[i] ))) break;
  2537. if (images[i])
  2538. {
  2539. output( "%s.%s: %s\n", src_dir_path( make, obj ), images[i], source->filename );
  2540. output( "\tCONVERT=\"%s\" ICOTOOL=\"%s\" RSVG=\"%s\" %s %s $@\n", convert, icotool, rsvg,
  2541. root_src_dir_path( "tools/buildimage" ), source->filename );
  2542. }
  2543. }
  2544. }
  2545. /*******************************************************************
  2546. * output_source_nls
  2547. */
  2548. static void output_source_nls( struct makefile *make, struct incl_file *source, const char *obj )
  2549. {
  2550. add_install_rule( make, source->name, source->name,
  2551. strmake( "D$(nlsdir)/%s", source->name ));
  2552. output_srcdir_symlink( make, strmake( "%s.nls", obj ));
  2553. }
  2554. /*******************************************************************
  2555. * output_source_desktop
  2556. */
  2557. static void output_source_desktop( struct makefile *make, struct incl_file *source, const char *obj )
  2558. {
  2559. add_install_rule( make, source->name, source->name,
  2560. strmake( "D$(datadir)/applications/%s", source->name ));
  2561. }
  2562. /*******************************************************************
  2563. * output_source_po
  2564. */
  2565. static void output_source_po( struct makefile *make, struct incl_file *source, const char *obj )
  2566. {
  2567. output( "%s.mo: %s\n", obj_dir_path( make, obj ), source->filename );
  2568. output( "\t%s -o $@ %s\n", msgfmt, source->filename );
  2569. strarray_add( &make->all_targets, strmake( "%s.mo", obj ));
  2570. }
  2571. /*******************************************************************
  2572. * output_source_in
  2573. */
  2574. static void output_source_in( struct makefile *make, struct incl_file *source, const char *obj )
  2575. {
  2576. unsigned int i;
  2577. if (strendswith( obj, ".man" ) && source->file->args)
  2578. {
  2579. struct strarray symlinks;
  2580. char *dir, *dest = replace_extension( obj, ".man", "" );
  2581. char *lang = strchr( dest, '.' );
  2582. char *section = source->file->args;
  2583. if (lang)
  2584. {
  2585. *lang++ = 0;
  2586. dir = strmake( "$(mandir)/%s/man%s", lang, section );
  2587. }
  2588. else dir = strmake( "$(mandir)/man%s", section );
  2589. add_install_rule( make, dest, xstrdup(obj), strmake( "d%s/%s.%s", dir, dest, section ));
  2590. symlinks = get_expanded_file_local_var( make, dest, "SYMLINKS" );
  2591. for (i = 0; i < symlinks.count; i++)
  2592. add_install_rule( make, symlinks.str[i], strmake( "%s.%s", dest, section ),
  2593. strmake( "y%s/%s.%s", dir, symlinks.str[i], section ));
  2594. free( dest );
  2595. free( dir );
  2596. }
  2597. strarray_add( &make->in_files, xstrdup(obj) );
  2598. strarray_add( &make->all_targets, xstrdup(obj) );
  2599. output( "%s: %s\n", obj_dir_path( make, obj ), source->filename );
  2600. output( "\t%s %s >$@ || (rm -f $@ && false)\n", sed_cmd, source->filename );
  2601. output( "%s:", obj_dir_path( make, obj ));
  2602. output_filenames( source->dependencies );
  2603. output( "\n" );
  2604. add_install_rule( make, obj, xstrdup( obj ), strmake( "d$(datadir)/wine/%s", obj ));
  2605. }
  2606. /*******************************************************************
  2607. * output_source_spec
  2608. */
  2609. static void output_source_spec( struct makefile *make, struct incl_file *source, const char *obj )
  2610. {
  2611. struct strarray imports = get_expanded_file_local_var( make, obj, "IMPORTS" );
  2612. struct strarray dll_flags = get_expanded_file_local_var( make, obj, "EXTRADLLFLAGS" );
  2613. struct strarray all_libs, dep_libs = empty_strarray;
  2614. char *dll_name, *obj_name, *output_file;
  2615. const char *debug_file;
  2616. if (!imports.count) imports = make->imports;
  2617. else if (make->use_msvcrt) add_crt_import( make, &imports );
  2618. if (!dll_flags.count) dll_flags = make->extradllflags;
  2619. all_libs = add_import_libs( make, &dep_libs, imports, 0, 0 );
  2620. add_import_libs( make, &dep_libs, get_default_imports( make ), 0, 0 ); /* dependencies only */
  2621. dll_name = strmake( "%s.dll%s", obj, make->is_cross ? "" : dll_ext );
  2622. obj_name = strmake( "%s%s", obj_dir_path( make, obj ), make->is_cross ? ".cross.o" : ".o" );
  2623. output_file = obj_dir_path( make, dll_name );
  2624. strarray_add( &make->clean_files, dll_name );
  2625. strarray_add( &make->res_files, strmake( "%s.res", obj ));
  2626. output( "%s.res: %s\n", obj_dir_path( make, obj ), obj_dir_path( make, dll_name ));
  2627. output( "\techo \"%s.dll TESTDLL \\\"%s\\\"\" | %s -u -o $@\n", obj, output_file,
  2628. tools_path( make, "wrc" ));
  2629. output( "%s:", output_file);
  2630. output_filename( source->filename );
  2631. output_filename( obj_name );
  2632. output_filenames( dep_libs );
  2633. output_filename( tools_path( make, "winebuild" ));
  2634. output_filename( tools_path( make, "winegcc" ));
  2635. output( "\n" );
  2636. output_winegcc_command( make, make->is_cross );
  2637. output_filename( "-s" );
  2638. output_filenames( dll_flags );
  2639. output_filename( "-shared" );
  2640. output_filename( source->filename );
  2641. output_filename( obj_name );
  2642. if ((debug_file = get_debug_file( make, output_file )))
  2643. output_filename( strmake( "-Wl,--debug-file,%s", debug_file ));
  2644. output_filenames( all_libs );
  2645. output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
  2646. output( "\n" );
  2647. }
  2648. /*******************************************************************
  2649. * output_source_default
  2650. */
  2651. static void output_source_default( struct makefile *make, struct incl_file *source, const char *obj )
  2652. {
  2653. struct strarray defines = get_source_defines( make, source, obj );
  2654. int is_dll_src = (make->testdll &&
  2655. strendswith( source->name, ".c" ) &&
  2656. find_src_file( make, replace_extension( source->name, ".c", ".spec" )));
  2657. int need_cross = (crosstarget &&
  2658. !(source->file->flags & FLAG_C_UNIX) &&
  2659. (make->is_cross ||
  2660. ((source->file->flags & FLAG_C_IMPLIB) &&
  2661. (needs_cross_lib( make ) || needs_delay_lib( make ))) ||
  2662. (make->staticlib && needs_cross_lib( make ))));
  2663. int need_obj = ((*dll_ext || !make->staticlib || !(source->file->flags & FLAG_C_UNIX)) &&
  2664. (!need_cross ||
  2665. (source->file->flags & FLAG_C_IMPLIB) ||
  2666. (make->module && make->staticlib)));
  2667. if ((source->file->flags & FLAG_GENERATED) &&
  2668. (!make->testdll || !strendswith( source->filename, "testlist.c" )))
  2669. strarray_add( &make->clean_files, source->basename );
  2670. if (source->file->flags & FLAG_C_IMPLIB) strarray_add( &make->implib_objs, strmake( "%s.o", obj ));
  2671. if (need_obj)
  2672. {
  2673. if ((source->file->flags & FLAG_C_UNIX) && *dll_ext)
  2674. strarray_add( &make->unixobj_files, strmake( "%s.o", obj ));
  2675. else if (!is_dll_src && (!(source->file->flags & FLAG_C_IMPLIB) || (make->importlib && strarray_exists( &make->imports, make->importlib ))))
  2676. strarray_add( &make->object_files, strmake( "%s.o", obj ));
  2677. else
  2678. strarray_add( &make->clean_files, strmake( "%s.o", obj ));
  2679. output( "%s.o: %s\n", obj_dir_path( make, obj ), source->filename );
  2680. output( "\t$(CC) -c -o $@ %s", source->filename );
  2681. output_filenames( defines );
  2682. if (make->module || make->staticlib || make->sharedlib || make->testdll)
  2683. {
  2684. output_filenames( dll_flags );
  2685. if (source->use_msvcrt) output_filenames( msvcrt_flags );
  2686. }
  2687. output_filenames( extra_cflags );
  2688. output_filenames( cpp_flags );
  2689. output_filename( "$(CFLAGS)" );
  2690. output( "\n" );
  2691. }
  2692. if (need_cross)
  2693. {
  2694. strarray_add( is_dll_src ? &make->clean_files : &make->crossobj_files, strmake( "%s.cross.o", obj ));
  2695. output( "%s.cross.o: %s\n", obj_dir_path( make, obj ), source->filename );
  2696. output( "\t$(CROSSCC) -c -o $@ %s", source->filename );
  2697. output_filenames( defines );
  2698. output_filenames( extra_cross_cflags );
  2699. if (source->file->flags & FLAG_C_IMPLIB) output_filename( "-fno-builtin" );
  2700. output_filenames( cpp_flags );
  2701. output_filename( "$(CROSSCFLAGS)" );
  2702. output( "\n" );
  2703. }
  2704. if (strendswith( source->name, ".c" ) && !(source->file->flags & FLAG_GENERATED))
  2705. {
  2706. strarray_add( &make->c2man_files, source->filename );
  2707. if (make->testdll && !is_dll_src)
  2708. {
  2709. strarray_add( &make->ok_files, strmake( "%s.ok", obj ));
  2710. output( "%s.ok:\n", obj_dir_path( make, obj ));
  2711. output( "\t%s $(RUNTESTFLAGS) -T . -M %s -p %s%s %s && touch $@\n",
  2712. root_src_dir_path( "tools/runtest" ), make->testdll,
  2713. obj_dir_path( make, replace_extension( make->testdll, ".dll", "_test.exe" )),
  2714. make->is_cross ? "" : dll_ext, obj );
  2715. }
  2716. }
  2717. if (need_obj) output_filename( strmake( "%s.o", obj_dir_path( make, obj )));
  2718. if (need_cross) output_filename( strmake( "%s.cross.o", obj_dir_path( make, obj )));
  2719. output( ":" );
  2720. output_filenames( source->dependencies );
  2721. output( "\n" );
  2722. }
  2723. /* dispatch table to output rules for a single source file */
  2724. static const struct
  2725. {
  2726. const char *ext;
  2727. void (*fn)( struct makefile *make, struct incl_file *source, const char *obj );
  2728. } output_source_funcs[] =
  2729. {
  2730. { "y", output_source_y },
  2731. { "l", output_source_l },
  2732. { "h", output_source_h },
  2733. { "rh", output_source_h },
  2734. { "inl", output_source_h },
  2735. { "rc", output_source_rc },
  2736. { "mc", output_source_mc },
  2737. { "res", output_source_res },
  2738. { "idl", output_source_idl },
  2739. { "tlb", output_source_tlb },
  2740. { "sfd", output_source_sfd },
  2741. { "svg", output_source_svg },
  2742. { "nls", output_source_nls },
  2743. { "desktop", output_source_desktop },
  2744. { "po", output_source_po },
  2745. { "in", output_source_in },
  2746. { "x", output_source_x },
  2747. { "spec", output_source_spec },
  2748. { NULL, output_source_default }
  2749. };
  2750. /*******************************************************************
  2751. * has_object_file
  2752. */
  2753. static int has_object_file( struct makefile *make )
  2754. {
  2755. struct incl_file *source;
  2756. int i;
  2757. LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
  2758. {
  2759. char *ext = get_extension( source->name );
  2760. if (!ext) fatal_error( "unsupported file type %s\n", source->name );
  2761. ext++;
  2762. for (i = 0; output_source_funcs[i].ext; i++)
  2763. if (!strcmp( ext, output_source_funcs[i].ext )) break;
  2764. if (!output_source_funcs[i].ext) return 1; /* default extension builds to an object file */
  2765. }
  2766. return 0;
  2767. }
  2768. /*******************************************************************
  2769. * output_man_pages
  2770. */
  2771. static void output_man_pages( struct makefile *make )
  2772. {
  2773. if (make->c2man_files.count)
  2774. {
  2775. char *spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
  2776. output( "manpages::\n" );
  2777. output( "\t%s -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
  2778. output_filename( strmake( "-R%s", root_src_dir_path( "" )));
  2779. output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
  2780. output_filename( strmake( "-o documentation/man%s", man_ext ));
  2781. output_filenames( make->c2man_files );
  2782. output( "\n" );
  2783. output( "htmlpages::\n" );
  2784. output( "\t%s -Th -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
  2785. output_filename( strmake( "-R%s", root_src_dir_path( "" )));
  2786. output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
  2787. output_filename( "-o documentation/html" );
  2788. output_filenames( make->c2man_files );
  2789. output( "\n" );
  2790. output( "sgmlpages::\n" );
  2791. output( "\t%s -Ts -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
  2792. output_filename( strmake( "-R%s", root_src_dir_path( "" )));
  2793. output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
  2794. output_filename( "-o documentation/api-guide" );
  2795. output_filenames( make->c2man_files );
  2796. output( "\n" );
  2797. output( "xmlpages::\n" );
  2798. output( "\t%s -Tx -w %s", root_src_dir_path( "tools/c2man.pl" ), spec_file );
  2799. output_filename( strmake( "-R%s", root_src_dir_path( "" )));
  2800. output_filename( strmake( "-I%s", root_src_dir_path( "include" )));
  2801. output_filename( "-o documentation/api-guide-xml" );
  2802. output_filenames( make->c2man_files );
  2803. output( "\n" );
  2804. strarray_add( &make->phony_targets, "manpages" );
  2805. strarray_add( &make->phony_targets, "htmlpages" );
  2806. strarray_add( &make->phony_targets, "sgmlpages" );
  2807. strarray_add( &make->phony_targets, "xmlpages" );
  2808. }
  2809. }
  2810. /*******************************************************************
  2811. * output_module
  2812. */
  2813. static void output_module( struct makefile *make )
  2814. {
  2815. struct strarray all_libs = empty_strarray;
  2816. struct strarray dep_libs = empty_strarray;
  2817. char *module_path = obj_dir_path( make, make->module );
  2818. const char *debug_file = NULL;
  2819. char *spec_file = NULL;
  2820. unsigned int i;
  2821. if (!make->is_exe) spec_file = src_dir_path( make, replace_extension( make->module, ".dll", ".spec" ));
  2822. strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->delayimports, 1, 0 ));
  2823. strarray_addall( &all_libs, add_import_libs( make, &dep_libs, make->imports, 0, 0 ));
  2824. add_import_libs( make, &dep_libs, get_default_imports( make ), 0, 0 ); /* dependencies only */
  2825. if (make->is_cross)
  2826. {
  2827. if (delay_load_flag)
  2828. {
  2829. for (i = 0; i < make->delayimports.count; i++)
  2830. strarray_add( &all_libs, strmake( "%s%s%s", delay_load_flag, make->delayimports.str[i],
  2831. strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
  2832. }
  2833. strarray_add( &make->all_targets, strmake( "%s", make->module ));
  2834. add_install_rule( make, make->module, strmake( "%s", make->module ),
  2835. strmake( "c$(dlldir)/%s", make->module ));
  2836. debug_file = get_debug_file( make, module_path );
  2837. output( "%s:", module_path );
  2838. }
  2839. else if (*dll_ext)
  2840. {
  2841. if (!make->use_msvcrt) strarray_addall( &all_libs, add_unix_libraries( make, &dep_libs ));
  2842. for (i = 0; i < make->delayimports.count; i++)
  2843. strarray_add( &all_libs, strmake( "-Wl,-delayload,%s%s", make->delayimports.str[i],
  2844. strchr( make->delayimports.str[i], '.' ) ? "" : ".dll" ));
  2845. strarray_add( &make->all_targets, strmake( "%s%s", make->module, dll_ext ));
  2846. strarray_add( &make->all_targets, strmake( "%s.fake", make->module ));
  2847. add_install_rule( make, make->module, strmake( "%s%s", make->module, dll_ext ),
  2848. strmake( "p$(dlldir)/%s%s", make->module, dll_ext ));
  2849. add_install_rule( make, make->module, strmake( "%s.fake", make->module ),
  2850. strmake( "d$(dlldir)/fakedlls/%s", make->module ));
  2851. output( "%s%s %s.fake:", module_path, dll_ext, module_path );
  2852. }
  2853. else
  2854. {
  2855. strarray_addall( &all_libs, add_unix_libraries( make, &dep_libs ));
  2856. strarray_add( &make->all_targets, make->module );
  2857. add_install_rule( make, make->module, make->module,
  2858. strmake( "p$(%s)/%s", spec_file ? "dlldir" : "bindir", make->module ));
  2859. debug_file = get_debug_file( make, module_path );
  2860. output( "%s:", module_path );
  2861. }
  2862. if (spec_file) output_filename( spec_file );
  2863. output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
  2864. output_filenames_obj_dir( make, make->res_files );
  2865. output_filenames( dep_libs );
  2866. output_filename( tools_path( make, "winebuild" ));
  2867. output_filename( tools_path( make, "winegcc" ));
  2868. output( "\n" );
  2869. output_winegcc_command( make, make->is_cross );
  2870. if (make->is_cross) output_filename( "-Wl,--wine-builtin" );
  2871. if (spec_file)
  2872. {
  2873. output_filename( "-shared" );
  2874. output_filename( spec_file );
  2875. }
  2876. output_filenames( make->extradllflags );
  2877. output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
  2878. output_filenames_obj_dir( make, make->res_files );
  2879. if (debug_file) output_filename( strmake( "-Wl,--debug-file,%s", debug_file ));
  2880. output_filenames( all_libs );
  2881. output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
  2882. output( "\n" );
  2883. if (make->unixobj_files.count)
  2884. {
  2885. struct strarray unix_imports = empty_strarray;
  2886. struct strarray unix_libs = empty_strarray;
  2887. struct strarray unix_deps = empty_strarray;
  2888. char *ext, *unix_lib = xmalloc( strlen( make->module ) + strlen( dll_ext ) + 1 );
  2889. strcpy( unix_lib, make->module );
  2890. if ((ext = get_extension( unix_lib ))) *ext = 0;
  2891. strcat( unix_lib, dll_ext );
  2892. if (!strarray_exists( &make->extradllflags, "-nodefaultlibs" ))
  2893. strarray_add( &unix_imports, "ntdll" );
  2894. strarray_add( &unix_imports, "winecrt0" );
  2895. strarray_addall( &unix_libs, add_import_libs( make, &unix_deps, unix_imports, 0, 1 ));
  2896. strarray_addall( &unix_libs, add_unix_libraries( make, &unix_deps ));
  2897. strarray_add( &make->all_targets, unix_lib );
  2898. add_install_rule( make, make->module, unix_lib, strmake( "p$(dlldir)/%s", unix_lib ));
  2899. output( "%s:", obj_dir_path( make, unix_lib ));
  2900. if (spec_file) output_filename( spec_file );
  2901. output_filenames_obj_dir( make, make->unixobj_files );
  2902. output_filenames( unix_deps );
  2903. output_filename( tools_path( make, "winebuild" ));
  2904. output_filename( tools_path( make, "winegcc" ));
  2905. output( "\n" );
  2906. output_winegcc_command( make, 0 );
  2907. output_filename( "-munix" );
  2908. output_filename( "-shared" );
  2909. if (spec_file) output_filename( spec_file );
  2910. if (strarray_exists( &make->extradllflags, "-nodefaultlibs" )) output_filename( "-nodefaultlibs" );
  2911. output_filenames_obj_dir( make, make->unixobj_files );
  2912. output_filenames( unix_libs );
  2913. output_filename( "$(LDFLAGS)" );
  2914. output( "\n" );
  2915. }
  2916. if (spec_file && make->importlib)
  2917. {
  2918. char *importlib_path = obj_dir_path( make, strmake( "lib%s", make->importlib ));
  2919. if (*dll_ext && !make->implib_objs.count)
  2920. {
  2921. strarray_add( &make->clean_files, strmake( "lib%s.def", make->importlib ));
  2922. output( "%s.def: %s %s\n", importlib_path, tools_path( make, "winebuild" ), spec_file );
  2923. output( "\t%s -w --def -o $@", tools_path( make, "winebuild" ) );
  2924. output_filenames( target_flags );
  2925. if (make->is_win16) output_filename( "-m16" );
  2926. output_filename( "--export" );
  2927. output_filename( spec_file );
  2928. output( "\n" );
  2929. add_install_rule( make, make->importlib,
  2930. strmake( "lib%s.def", make->importlib ),
  2931. strmake( "d$(dlldir)/lib%s.def", make->importlib ));
  2932. }
  2933. else
  2934. {
  2935. strarray_add( &make->clean_files, strmake( "lib%s.a", make->importlib ));
  2936. if (!*dll_ext && needs_delay_lib( make ))
  2937. {
  2938. strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
  2939. output( "%s.delay.a ", importlib_path );
  2940. }
  2941. output( "%s.a: %s %s", importlib_path, tools_path( make, "winebuild" ), spec_file );
  2942. output_filenames_obj_dir( make, make->implib_objs );
  2943. output( "\n" );
  2944. output( "\t%s -w --implib -o $@", tools_path( make, "winebuild" ) );
  2945. output_filenames( target_flags );
  2946. if (make->is_win16) output_filename( "-m16" );
  2947. output_filename( "--export" );
  2948. output_filename( spec_file );
  2949. output_filenames_obj_dir( make, make->implib_objs );
  2950. output( "\n" );
  2951. add_install_rule( make, make->importlib,
  2952. strmake( "lib%s.a", make->importlib ),
  2953. strmake( "d$(dlldir)/lib%s.a", make->importlib ));
  2954. }
  2955. if (crosstarget && (needs_cross_lib( make ) || needs_delay_lib( make )))
  2956. {
  2957. struct strarray cross_files = strarray_replace_extension( &make->implib_objs, ".o", ".cross.o" );
  2958. if (needs_cross_lib( make ))
  2959. {
  2960. strarray_add( &make->clean_files, strmake( "lib%s.cross.a", make->importlib ));
  2961. output_filename( strmake( "%s.cross.a", importlib_path ));
  2962. }
  2963. if (needs_delay_lib( make ))
  2964. {
  2965. strarray_add( &make->clean_files, strmake( "lib%s.delay.a", make->importlib ));
  2966. output_filename( strmake( "%s.delay.a", importlib_path ));
  2967. }
  2968. output( ": %s %s", tools_path( make, "winebuild" ), spec_file );
  2969. output_filenames_obj_dir( make, cross_files );
  2970. output( "\n" );
  2971. output( "\t%s -b %s -w --implib -o $@", tools_path( make, "winebuild" ), crosstarget );
  2972. if (make->is_win16) output_filename( "-m16" );
  2973. output_filename( "--export" );
  2974. output_filename( spec_file );
  2975. output_filenames_obj_dir( make, cross_files );
  2976. output( "\n" );
  2977. }
  2978. if (needs_implib_symlink( make ))
  2979. strarray_addall( &top_makefile->clean_files, output_importlib_symlinks( make ));
  2980. }
  2981. if (spec_file)
  2982. output_man_pages( make );
  2983. else if (*dll_ext && !make->is_win16 && strendswith( make->module, ".exe" ))
  2984. {
  2985. char *binary = replace_extension( make->module, ".exe", "" );
  2986. add_install_rule( make, binary, "wineapploader", strmake( "t$(bindir)/%s", binary ));
  2987. }
  2988. }
  2989. /*******************************************************************
  2990. * output_static_lib
  2991. */
  2992. static void output_static_lib( struct makefile *make )
  2993. {
  2994. strarray_add( &make->all_targets, make->staticlib );
  2995. output( "%s:", obj_dir_path( make, make->staticlib ));
  2996. output_filenames_obj_dir( make, make->object_files );
  2997. output_filenames_obj_dir( make, make->unixobj_files );
  2998. output( "\n\trm -f $@\n" );
  2999. output( "\t%s rc $@", ar );
  3000. output_filenames_obj_dir( make, make->object_files );
  3001. output_filenames_obj_dir( make, make->unixobj_files );
  3002. output( "\n\t%s $@\n", ranlib );
  3003. add_install_rule( make, make->staticlib, make->staticlib,
  3004. strmake( "d$(dlldir)/%s", make->staticlib ));
  3005. if (needs_cross_lib( make ))
  3006. {
  3007. char *name = replace_extension( make->staticlib, ".a", ".cross.a" );
  3008. strarray_add( &make->all_targets, name );
  3009. output( "%s: %s", obj_dir_path( make, name ), tools_path( make, "winebuild" ));
  3010. output_filenames_obj_dir( make, make->crossobj_files );
  3011. output( "\n" );
  3012. output( "\t%s -b %s -w --staticlib -o $@", tools_path( make, "winebuild" ), crosstarget );
  3013. output_filenames_obj_dir( make, make->crossobj_files );
  3014. output( "\n" );
  3015. }
  3016. }
  3017. /*******************************************************************
  3018. * output_shared_lib
  3019. */
  3020. static void output_shared_lib( struct makefile *make )
  3021. {
  3022. unsigned int i;
  3023. char *basename, *p;
  3024. struct strarray names = get_shared_lib_names( make->sharedlib );
  3025. struct strarray all_libs = empty_strarray;
  3026. struct strarray dep_libs = empty_strarray;
  3027. basename = xstrdup( make->sharedlib );
  3028. if ((p = strchr( basename, '.' ))) *p = 0;
  3029. strarray_addall( &dep_libs, get_local_dependencies( make, basename, make->in_files ));
  3030. strarray_addall( &all_libs, get_expanded_file_local_var( make, basename, "LDFLAGS" ));
  3031. strarray_addall( &all_libs, add_unix_libraries( make, &dep_libs ));
  3032. output( "%s:", obj_dir_path( make, make->sharedlib ));
  3033. output_filenames_obj_dir( make, make->object_files );
  3034. output_filenames( dep_libs );
  3035. output( "\n" );
  3036. output( "\t$(CC) -o $@" );
  3037. output_filenames_obj_dir( make, make->object_files );
  3038. output_filenames( all_libs );
  3039. output_filename( "$(LDFLAGS)" );
  3040. output( "\n" );
  3041. add_install_rule( make, make->sharedlib, make->sharedlib,
  3042. strmake( "p$(libdir)/%s", make->sharedlib ));
  3043. for (i = 1; i < names.count; i++)
  3044. {
  3045. output( "%s: %s\n", obj_dir_path( make, names.str[i] ), obj_dir_path( make, names.str[i-1] ));
  3046. output_symlink_rule( names.str[i-1], obj_dir_path( make, names.str[i] ));
  3047. add_install_rule( make, names.str[i], names.str[i-1],
  3048. strmake( "y$(libdir)/%s", names.str[i] ));
  3049. }
  3050. strarray_addall( &make->all_targets, names );
  3051. }
  3052. /*******************************************************************
  3053. * output_test_module
  3054. */
  3055. static void output_test_module( struct makefile *make )
  3056. {
  3057. char *testmodule = replace_extension( make->testdll, ".dll", "_test.exe" );
  3058. char *stripped = replace_extension( make->testdll, ".dll", "_test-stripped.exe" );
  3059. char *testres = replace_extension( make->testdll, ".dll", "_test.res" );
  3060. struct strarray dep_libs = empty_strarray;
  3061. struct strarray all_libs = add_import_libs( make, &dep_libs, make->imports, 0, 0 );
  3062. struct makefile *parent = get_parent_makefile( make );
  3063. const char *ext = make->is_cross ? "" : dll_ext;
  3064. const char *parent_ext = parent && parent->is_cross ? "" : dll_ext;
  3065. const char *debug_file;
  3066. char *output_file;
  3067. add_import_libs( make, &dep_libs, get_default_imports( make ), 0, 0 ); /* dependencies only */
  3068. strarray_add( &make->all_targets, strmake( "%s%s", testmodule, ext ));
  3069. strarray_add( &make->clean_files, strmake( "%s%s", stripped, ext ));
  3070. output_file = strmake( "%s%s", obj_dir_path( make, testmodule ), ext );
  3071. output( "%s:\n", output_file );
  3072. output_winegcc_command( make, make->is_cross );
  3073. output_filenames( make->extradllflags );
  3074. output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
  3075. output_filenames_obj_dir( make, make->res_files );
  3076. if ((debug_file = get_debug_file( make, output_file )))
  3077. output_filename( strmake( "-Wl,--debug-file,%s", debug_file ));
  3078. output_filenames( all_libs );
  3079. output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
  3080. output( "\n" );
  3081. output( "%s%s:\n", obj_dir_path( make, stripped ), ext );
  3082. output_winegcc_command( make, make->is_cross );
  3083. output_filename( "-s" );
  3084. output_filename( strmake( "-Wb,-F,%s", testmodule ));
  3085. output_filenames( make->extradllflags );
  3086. output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
  3087. output_filenames_obj_dir( make, make->res_files );
  3088. output_filenames( all_libs );
  3089. output_filename( make->is_cross ? "$(CROSSLDFLAGS)" : "$(LDFLAGS)" );
  3090. output( "\n" );
  3091. output( "%s%s %s%s:", obj_dir_path( make, testmodule ), ext, obj_dir_path( make, stripped ), ext );
  3092. output_filenames_obj_dir( make, make->is_cross ? make->crossobj_files : make->object_files );
  3093. output_filenames_obj_dir( make, make->res_files );
  3094. output_filenames( dep_libs );
  3095. output_filename( tools_path( make, "winebuild" ));
  3096. output_filename( tools_path( make, "winegcc" ));
  3097. output( "\n" );
  3098. output( "programs/winetest/%s: %s%s\n", testres, obj_dir_path( make, stripped ), ext );
  3099. output( "\techo \"%s TESTRES \\\"%s%s\\\"\" | %s -u -o $@\n",
  3100. testmodule, obj_dir_path( make, stripped ), ext, tools_path( make, "wrc" ));
  3101. output_filenames_obj_dir( make, make->ok_files );
  3102. output( ": %s%s", obj_dir_path( make, testmodule ), ext );
  3103. if (parent) output( " %s%s", obj_dir_path( parent, make->testdll ), parent_ext );
  3104. output( "\n" );
  3105. output( "%s %s:", obj_dir_path( make, "check" ), obj_dir_path( make, "test" ));
  3106. if (!make->disabled && parent && !parent->disabled) output_filenames_obj_dir( make, make->ok_files );
  3107. output( "\n" );
  3108. strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "check" ));
  3109. strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "test" ));
  3110. output( "%s::\n", obj_dir_path( make, "testclean" ));
  3111. output( "\trm -f" );
  3112. output_filenames_obj_dir( make, make->ok_files );
  3113. output( "\n" );
  3114. strarray_addall( &make->clean_files, make->ok_files );
  3115. strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "testclean" ));
  3116. }
  3117. /*******************************************************************
  3118. * output_programs
  3119. */
  3120. static void output_programs( struct makefile *make )
  3121. {
  3122. unsigned int i, j;
  3123. for (i = 0; i < make->programs.count; i++)
  3124. {
  3125. char *program_installed = NULL;
  3126. char *program = strmake( "%s%s", make->programs.str[i], exe_ext );
  3127. struct strarray deps = get_local_dependencies( make, make->programs.str[i], make->in_files );
  3128. struct strarray all_libs = get_expanded_file_local_var( make, make->programs.str[i], "LDFLAGS" );
  3129. struct strarray objs = get_expanded_file_local_var( make, make->programs.str[i], "OBJS" );
  3130. struct strarray symlinks = get_expanded_file_local_var( make, make->programs.str[i], "SYMLINKS" );
  3131. if (!objs.count) objs = make->object_files;
  3132. if (!strarray_exists( &all_libs, "-nodefaultlibs" ))
  3133. strarray_addall( &all_libs, add_unix_libraries( make, &deps ));
  3134. output( "%s:", obj_dir_path( make, program ) );
  3135. output_filenames_obj_dir( make, objs );
  3136. output_filenames( deps );
  3137. output( "\n" );
  3138. output( "\t$(CC) -o $@" );
  3139. output_filenames_obj_dir( make, objs );
  3140. output_filenames( all_libs );
  3141. output_filename( "$(LDFLAGS)" );
  3142. output( "\n" );
  3143. strarray_add( &make->all_targets, program );
  3144. for (j = 0; j < symlinks.count; j++)
  3145. {
  3146. output( "%s: %s\n", obj_dir_path( make, symlinks.str[j] ), obj_dir_path( make, program ));
  3147. output_symlink_rule( program, obj_dir_path( make, symlinks.str[j] ));
  3148. }
  3149. strarray_addall( &make->all_targets, symlinks );
  3150. add_install_rule( make, program, program_installed ? program_installed : program,
  3151. strmake( "p$(bindir)/%s", program ));
  3152. for (j = 0; j < symlinks.count; j++)
  3153. add_install_rule( make, symlinks.str[j], program,
  3154. strmake( "y$(bindir)/%s%s", symlinks.str[j], exe_ext ));
  3155. }
  3156. }
  3157. /*******************************************************************
  3158. * output_subdirs
  3159. */
  3160. static void output_subdirs( struct makefile *make )
  3161. {
  3162. struct strarray all_targets = empty_strarray;
  3163. struct strarray makefile_deps = empty_strarray;
  3164. struct strarray clean_files = empty_strarray;
  3165. struct strarray testclean_files = empty_strarray;
  3166. struct strarray distclean_files = empty_strarray;
  3167. struct strarray dependencies = empty_strarray;
  3168. struct strarray install_lib_deps = empty_strarray;
  3169. struct strarray install_dev_deps = empty_strarray;
  3170. struct strarray tooldeps_deps = empty_strarray;
  3171. struct strarray buildtest_deps = empty_strarray;
  3172. unsigned int i;
  3173. strarray_addall( &clean_files, make->clean_files );
  3174. strarray_addall( &distclean_files, make->distclean_files );
  3175. strarray_addall( &all_targets, make->all_targets );
  3176. for (i = 0; i < subdirs.count; i++)
  3177. {
  3178. strarray_add( &makefile_deps, src_dir_path( submakes[i], strmake ( "%s.in", output_makefile_name )));
  3179. strarray_addall_uniq( &make->phony_targets, submakes[i]->phony_targets );
  3180. strarray_addall_uniq( &make->uninstall_files, submakes[i]->uninstall_files );
  3181. strarray_addall_uniq( &dependencies, submakes[i]->dependencies );
  3182. strarray_addall_path( &clean_files, submakes[i]->obj_dir, submakes[i]->clean_files );
  3183. strarray_addall_path( &distclean_files, submakes[i]->obj_dir, submakes[i]->distclean_files );
  3184. strarray_addall_path( &testclean_files, submakes[i]->obj_dir, submakes[i]->ok_files );
  3185. strarray_addall_path( &make->pot_files, submakes[i]->obj_dir, submakes[i]->pot_files );
  3186. if (submakes[i]->disabled) continue;
  3187. strarray_addall_path( &all_targets, submakes[i]->obj_dir, submakes[i]->all_targets );
  3188. if (!strcmp( submakes[i]->obj_dir, "tools" ) || !strncmp( submakes[i]->obj_dir, "tools/", 6 ))
  3189. strarray_add( &tooldeps_deps, obj_dir_path( submakes[i], "all" ));
  3190. if (submakes[i]->testdll)
  3191. strarray_add( &buildtest_deps, obj_dir_path( submakes[i], "all" ));
  3192. if (submakes[i]->install_rules[INSTALL_LIB].count)
  3193. strarray_add( &install_lib_deps, obj_dir_path( submakes[i], "install-lib" ));
  3194. if (submakes[i]->install_rules[INSTALL_DEV].count)
  3195. strarray_add( &install_dev_deps, obj_dir_path( submakes[i], "install-dev" ));
  3196. }
  3197. strarray_addall( &dependencies, makefile_deps );
  3198. output( "all:" );
  3199. output_filenames( all_targets );
  3200. output( "\n" );
  3201. output( "Makefile:" );
  3202. output_filenames( makefile_deps );
  3203. output( "\n" );
  3204. output_filenames( dependencies );
  3205. output( ":\n" );
  3206. if (install_lib_deps.count)
  3207. {
  3208. output( "install install-lib::" );
  3209. output_filenames( install_lib_deps );
  3210. output( "\n" );
  3211. strarray_add_uniq( &make->phony_targets, "install" );
  3212. strarray_add_uniq( &make->phony_targets, "install-lib" );
  3213. }
  3214. if (install_dev_deps.count)
  3215. {
  3216. output( "install install-dev::" );
  3217. output_filenames( install_dev_deps );
  3218. output( "\n" );
  3219. strarray_add_uniq( &make->phony_targets, "install" );
  3220. strarray_add_uniq( &make->phony_targets, "install-dev" );
  3221. }
  3222. output_uninstall_rules( make );
  3223. if (buildtest_deps.count)
  3224. {
  3225. output( "buildtests:" );
  3226. output_filenames( buildtest_deps );
  3227. output( "\n" );
  3228. strarray_add_uniq( &make->phony_targets, "buildtests" );
  3229. }
  3230. output( "check test:" );
  3231. output_filenames( testclean_files );
  3232. output( "\n" );
  3233. strarray_add_uniq( &make->phony_targets, "check" );
  3234. strarray_add_uniq( &make->phony_targets, "test" );
  3235. output( "clean::\n");
  3236. output_rm_filenames( clean_files );
  3237. output( "testclean::\n");
  3238. output_rm_filenames( testclean_files );
  3239. output( "distclean::\n");
  3240. output_rm_filenames( distclean_files );
  3241. strarray_add_uniq( &make->phony_targets, "distclean" );
  3242. strarray_add_uniq( &make->phony_targets, "testclean" );
  3243. if (tooldeps_deps.count)
  3244. {
  3245. output( "__tooldeps__:" );
  3246. output_filenames( tooldeps_deps );
  3247. output( "\n" );
  3248. strarray_add_uniq( &make->phony_targets, "__tooldeps__" );
  3249. }
  3250. if (get_expanded_make_variable( make, "GETTEXTPO_LIBS" )) output_po_files( make );
  3251. if (make->phony_targets.count)
  3252. {
  3253. output( ".PHONY:" );
  3254. output_filenames( make->phony_targets );
  3255. output( "\n" );
  3256. }
  3257. }
  3258. /*******************************************************************
  3259. * output_sources
  3260. */
  3261. static void output_sources( struct makefile *make )
  3262. {
  3263. struct strarray all_targets = empty_strarray;
  3264. struct incl_file *source;
  3265. unsigned int i, j;
  3266. strarray_add_uniq( &make->phony_targets, "all" );
  3267. LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
  3268. {
  3269. char *obj = xstrdup( source->name );
  3270. char *ext = get_extension( obj );
  3271. if (!ext) fatal_error( "unsupported file type %s\n", source->name );
  3272. *ext++ = 0;
  3273. for (j = 0; output_source_funcs[j].ext; j++)
  3274. if (!strcmp( ext, output_source_funcs[j].ext )) break;
  3275. output_source_funcs[j].fn( make, source, obj );
  3276. strarray_addall_uniq( &make->dependencies, source->dependencies );
  3277. }
  3278. /* special case for winetest: add resource files from other test dirs */
  3279. if (make->obj_dir && !strcmp( make->obj_dir, "programs/winetest" ))
  3280. {
  3281. struct strarray tests = enable_tests;
  3282. if (!tests.count)
  3283. for (i = 0; i < subdirs.count; i++)
  3284. if (submakes[i]->testdll && !submakes[i]->disabled)
  3285. strarray_add( &tests, submakes[i]->testdll );
  3286. for (i = 0; i < tests.count; i++)
  3287. strarray_add( &make->res_files, replace_extension( tests.str[i], ".dll", "_test.res" ));
  3288. }
  3289. if (make->dlldata_files.count)
  3290. {
  3291. output( "%s: %s %s\n", obj_dir_path( make, "dlldata.c" ),
  3292. tools_path( make, "widl" ), src_dir_path( make, "Makefile.in" ));
  3293. output( "\t%s --dlldata-only -o $@", tools_path( make, "widl" ));
  3294. output_filenames( make->dlldata_files );
  3295. output( "\n" );
  3296. }
  3297. if (make->staticlib) output_static_lib( make );
  3298. else if (make->module) output_module( make );
  3299. else if (make->testdll) output_test_module( make );
  3300. else if (make->sharedlib) output_shared_lib( make );
  3301. else if (make->programs.count) output_programs( make );
  3302. for (i = 0; i < make->scripts.count; i++)
  3303. add_install_rule( make, make->scripts.str[i], make->scripts.str[i],
  3304. strmake( "S$(bindir)/%s", make->scripts.str[i] ));
  3305. for (i = 0; i < make->extra_targets.count; i++)
  3306. if (strarray_exists( &make->dependencies, obj_dir_path( make, make->extra_targets.str[i] )))
  3307. strarray_add( &make->clean_files, make->extra_targets.str[i] );
  3308. else
  3309. strarray_add( &make->all_targets, make->extra_targets.str[i] );
  3310. if (!make->src_dir) strarray_add( &make->distclean_files, ".gitignore" );
  3311. strarray_add( &make->distclean_files, "Makefile" );
  3312. if (make->testdll) strarray_add( &make->distclean_files, "testlist.c" );
  3313. if (!make->obj_dir)
  3314. strarray_addall( &make->distclean_files, get_expanded_make_var_array( make, "CONFIGURE_TARGETS" ));
  3315. else if (!strcmp( make->obj_dir, "po" ))
  3316. strarray_add( &make->distclean_files, "LINGUAS" );
  3317. strarray_addall( &make->clean_files, make->object_files );
  3318. strarray_addall( &make->clean_files, make->crossobj_files );
  3319. strarray_addall( &make->clean_files, make->unixobj_files );
  3320. strarray_addall( &make->clean_files, make->res_files );
  3321. strarray_addall( &make->clean_files, make->pot_files );
  3322. strarray_addall( &make->clean_files, make->debug_files );
  3323. strarray_addall( &make->clean_files, make->all_targets );
  3324. if (make == top_makefile)
  3325. {
  3326. output_subdirs( make );
  3327. return;
  3328. }
  3329. strarray_addall( &all_targets, make->all_targets );
  3330. strarray_addall( &all_targets, make->font_files );
  3331. if (all_targets.count)
  3332. {
  3333. output( "%s:", obj_dir_path( make, "all" ));
  3334. output_filenames_obj_dir( make, all_targets );
  3335. output( "\n" );
  3336. strarray_add_uniq( &make->phony_targets, obj_dir_path( make, "all" ));
  3337. }
  3338. output_install_rules( make, INSTALL_LIB, "install-lib" );
  3339. output_install_rules( make, INSTALL_DEV, "install-dev" );
  3340. if (make->clean_files.count)
  3341. {
  3342. output( "%s::\n", obj_dir_path( make, "clean" ));
  3343. output( "\trm -f" );
  3344. output_filenames_obj_dir( make, make->clean_files );
  3345. output( "\n" );
  3346. strarray_add( &make->phony_targets, obj_dir_path( make, "clean" ));
  3347. }
  3348. }
  3349. /*******************************************************************
  3350. * create_temp_file
  3351. */
  3352. static FILE *create_temp_file( const char *orig )
  3353. {
  3354. char *name = xmalloc( strlen(orig) + 13 );
  3355. unsigned int i, id = getpid();
  3356. int fd;
  3357. FILE *ret = NULL;
  3358. for (i = 0; i < 100; i++)
  3359. {
  3360. sprintf( name, "%s.tmp%08x", orig, id );
  3361. if ((fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0666 )) != -1)
  3362. {
  3363. ret = fdopen( fd, "w" );
  3364. break;
  3365. }
  3366. if (errno != EEXIST) break;
  3367. id += 7777;
  3368. }
  3369. if (!ret) fatal_error( "failed to create output file for '%s'\n", orig );
  3370. temp_file_name = name;
  3371. return ret;
  3372. }
  3373. /*******************************************************************
  3374. * rename_temp_file
  3375. */
  3376. static void rename_temp_file( const char *dest )
  3377. {
  3378. int ret = rename( temp_file_name, dest );
  3379. if (ret == -1 && errno == EEXIST)
  3380. {
  3381. /* rename doesn't overwrite on windows */
  3382. unlink( dest );
  3383. ret = rename( temp_file_name, dest );
  3384. }
  3385. if (ret == -1) fatal_error( "failed to rename output file to '%s'\n", dest );
  3386. temp_file_name = NULL;
  3387. }
  3388. /*******************************************************************
  3389. * are_files_identical
  3390. */
  3391. static int are_files_identical( FILE *file1, FILE *file2 )
  3392. {
  3393. for (;;)
  3394. {
  3395. char buffer1[8192], buffer2[8192];
  3396. int size1 = fread( buffer1, 1, sizeof(buffer1), file1 );
  3397. int size2 = fread( buffer2, 1, sizeof(buffer2), file2 );
  3398. if (size1 != size2) return 0;
  3399. if (!size1) return feof( file1 ) && feof( file2 );
  3400. if (memcmp( buffer1, buffer2, size1 )) return 0;
  3401. }
  3402. }
  3403. /*******************************************************************
  3404. * rename_temp_file_if_changed
  3405. */
  3406. static void rename_temp_file_if_changed( const char *dest )
  3407. {
  3408. FILE *file1, *file2;
  3409. int do_rename = 1;
  3410. if ((file1 = fopen( dest, "r" )))
  3411. {
  3412. if ((file2 = fopen( temp_file_name, "r" )))
  3413. {
  3414. do_rename = !are_files_identical( file1, file2 );
  3415. fclose( file2 );
  3416. }
  3417. fclose( file1 );
  3418. }
  3419. if (!do_rename)
  3420. {
  3421. unlink( temp_file_name );
  3422. temp_file_name = NULL;
  3423. }
  3424. else rename_temp_file( dest );
  3425. }
  3426. /*******************************************************************
  3427. * output_linguas
  3428. */
  3429. static void output_linguas( const struct makefile *make )
  3430. {
  3431. const char *dest = obj_dir_path( make, "LINGUAS" );
  3432. struct incl_file *source;
  3433. output_file = create_temp_file( dest );
  3434. output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
  3435. LIST_FOR_EACH_ENTRY( source, &make->sources, struct incl_file, entry )
  3436. if (strendswith( source->name, ".po" ))
  3437. output( "%s\n", replace_extension( source->name, ".po", "" ));
  3438. if (fclose( output_file )) fatal_perror( "write" );
  3439. output_file = NULL;
  3440. rename_temp_file_if_changed( dest );
  3441. }
  3442. /*******************************************************************
  3443. * output_testlist
  3444. */
  3445. static void output_testlist( const struct makefile *make )
  3446. {
  3447. const char *dest = obj_dir_path( make, "testlist.c" );
  3448. struct strarray files = empty_strarray;
  3449. unsigned int i;
  3450. for (i = 0; i < make->ok_files.count; i++)
  3451. strarray_add( &files, replace_extension( make->ok_files.str[i], ".ok", "" ));
  3452. output_file = create_temp_file( dest );
  3453. output( "/* Automatically generated by make depend; DO NOT EDIT!! */\n\n" );
  3454. output( "#define WIN32_LEAN_AND_MEAN\n" );
  3455. output( "#include <windows.h>\n\n" );
  3456. output( "#define STANDALONE\n" );
  3457. output( "#include \"wine/test.h\"\n\n" );
  3458. for (i = 0; i < files.count; i++) output( "extern void func_%s(void);\n", files.str[i] );
  3459. output( "\n" );
  3460. output( "const struct test winetest_testlist[] =\n" );
  3461. output( "{\n" );
  3462. for (i = 0; i < files.count; i++) output( " { \"%s\", func_%s },\n", files.str[i], files.str[i] );
  3463. output( " { 0, 0 }\n" );
  3464. output( "};\n" );
  3465. if (fclose( output_file )) fatal_perror( "write" );
  3466. output_file = NULL;
  3467. rename_temp_file_if_changed( dest );
  3468. }
  3469. /*******************************************************************
  3470. * output_gitignore
  3471. */
  3472. static void output_gitignore( const char *dest, struct strarray files )
  3473. {
  3474. int i;
  3475. output_file = create_temp_file( dest );
  3476. output( "# Automatically generated by make depend; DO NOT EDIT!!\n" );
  3477. for (i = 0; i < files.count; i++)
  3478. {
  3479. if (!strchr( files.str[i], '/' )) output( "/" );
  3480. output( "%s\n", files.str[i] );
  3481. }
  3482. if (fclose( output_file )) fatal_perror( "write" );
  3483. output_file = NULL;
  3484. rename_temp_file( dest );
  3485. }
  3486. /*******************************************************************
  3487. * output_stub_makefile
  3488. */
  3489. static void output_stub_makefile( struct makefile *make )
  3490. {
  3491. struct strarray targets = empty_strarray;
  3492. const char *make_var = strarray_get_value( &top_makefile->vars, "MAKE" );
  3493. if (make_var) output( "MAKE = %s\n\n", make_var );
  3494. output( "all:\n" );
  3495. if (make->all_targets.count) strarray_add( &targets, "all" );
  3496. if (make->install_rules[INSTALL_LIB].count || make->install_rules[INSTALL_DEV].count)
  3497. strarray_add( &targets, "install" );
  3498. if (make->install_rules[INSTALL_LIB].count) strarray_add( &targets, "install-lib" );
  3499. if (make->install_rules[INSTALL_DEV].count) strarray_add( &targets, "install-dev" );
  3500. if (make->clean_files.count) strarray_add( &targets, "clean" );
  3501. if (make->ok_files.count)
  3502. {
  3503. strarray_add( &targets, "check" );
  3504. strarray_add( &targets, "test" );
  3505. strarray_add( &targets, "testclean" );
  3506. }
  3507. output_filenames( targets );
  3508. output( ":\n" );
  3509. output( "\t@cd %s && $(MAKE) %s/$@\n", get_relative_path( make->obj_dir, "" ), make->obj_dir );
  3510. output( ".PHONY:" );
  3511. output_filenames( targets );
  3512. output( "\n" );
  3513. }
  3514. /*******************************************************************
  3515. * output_dependencies
  3516. */
  3517. static void output_dependencies( struct makefile *make )
  3518. {
  3519. struct strarray ignore_files = empty_strarray;
  3520. char buffer[1024];
  3521. FILE *src_file;
  3522. int i, found = 0;
  3523. if (make->obj_dir) create_dir( make->obj_dir );
  3524. output_file_name = obj_dir_path( make, output_makefile_name );
  3525. output_file = create_temp_file( output_file_name );
  3526. /* copy the contents of the source makefile */
  3527. src_file = open_input_makefile( make );
  3528. while (fgets( buffer, sizeof(buffer), src_file ) && !found)
  3529. {
  3530. if (fwrite( buffer, 1, strlen(buffer), output_file ) != strlen(buffer)) fatal_perror( "write" );
  3531. found = !strncmp( buffer, separator, strlen(separator) );
  3532. }
  3533. if (fclose( src_file )) fatal_perror( "close" );
  3534. input_file_name = NULL;
  3535. if (!found) output( "\n%s (everything below this line is auto-generated; DO NOT EDIT!!)\n", separator );
  3536. if (make == top_makefile)
  3537. {
  3538. for (i = 0; i < subdirs.count; i++) output_sources( submakes[i] );
  3539. output_sources( make );
  3540. }
  3541. else output_stub_makefile( make );
  3542. fclose( output_file );
  3543. output_file = NULL;
  3544. rename_temp_file( output_file_name );
  3545. strarray_addall( &ignore_files, make->distclean_files );
  3546. strarray_addall( &ignore_files, make->clean_files );
  3547. if (make->testdll) output_testlist( make );
  3548. if (make->obj_dir && !strcmp( make->obj_dir, "po" )) output_linguas( make );
  3549. if (!make->src_dir) output_gitignore( obj_dir_path( make, ".gitignore" ), ignore_files );
  3550. create_file_directories( make, ignore_files );
  3551. output_file_name = NULL;
  3552. }
  3553. /*******************************************************************
  3554. * load_sources
  3555. */
  3556. static void load_sources( struct makefile *make )
  3557. {
  3558. static const char *source_vars[] =
  3559. {
  3560. "SOURCES",
  3561. "C_SRCS",
  3562. "OBJC_SRCS",
  3563. "RC_SRCS",
  3564. "MC_SRCS",
  3565. "IDL_SRCS",
  3566. "BISON_SRCS",
  3567. "LEX_SRCS",
  3568. "HEADER_SRCS",
  3569. "XTEMPLATE_SRCS",
  3570. "SVG_SRCS",
  3571. "FONT_SRCS",
  3572. "IN_SRCS",
  3573. "PO_SRCS",
  3574. "MANPAGES",
  3575. NULL
  3576. };
  3577. const char **var;
  3578. unsigned int i;
  3579. struct strarray value;
  3580. struct incl_file *file;
  3581. strarray_set_value( &make->vars, "top_srcdir", root_src_dir_path( "" ));
  3582. strarray_set_value( &make->vars, "srcdir", src_dir_path( make, "" ));
  3583. make->parent_dir = get_expanded_make_variable( make, "PARENTSRC" );
  3584. make->module = get_expanded_make_variable( make, "MODULE" );
  3585. make->testdll = get_expanded_make_variable( make, "TESTDLL" );
  3586. make->sharedlib = get_expanded_make_variable( make, "SHAREDLIB" );
  3587. make->staticlib = get_expanded_make_variable( make, "STATICLIB" );
  3588. make->importlib = get_expanded_make_variable( make, "IMPORTLIB" );
  3589. make->programs = get_expanded_make_var_array( make, "PROGRAMS" );
  3590. make->scripts = get_expanded_make_var_array( make, "SCRIPTS" );
  3591. make->imports = get_expanded_make_var_array( make, "IMPORTS" );
  3592. make->delayimports = get_expanded_make_var_array( make, "DELAYIMPORTS" );
  3593. make->extradllflags = get_expanded_make_var_array( make, "EXTRADLLFLAGS" );
  3594. make->install_lib = get_expanded_make_var_array( make, "INSTALL_LIB" );
  3595. make->install_dev = get_expanded_make_var_array( make, "INSTALL_DEV" );
  3596. make->extra_targets = get_expanded_make_var_array( make, "EXTRA_TARGETS" );
  3597. if (make->module && strendswith( make->module, ".a" )) make->staticlib = make->module;
  3598. make->is_win16 = strarray_exists( &make->extradllflags, "-m16" );
  3599. if ((make->module && make->staticlib) || make->testdll || make->is_win16)
  3600. strarray_add_uniq( &make->extradllflags, "-mno-cygwin" );
  3601. strarray_addall( &make->extradllflags, get_expanded_make_var_array( make, "APPMODE" ));
  3602. make->disabled = make->obj_dir && strarray_exists( &disabled_dirs, make->obj_dir );
  3603. make->use_msvcrt = strarray_exists( &make->extradllflags, "-mno-cygwin" );
  3604. make->is_exe = strarray_exists( &make->extradllflags, "-mconsole" ) ||
  3605. strarray_exists( &make->extradllflags, "-mwindows" );
  3606. if (make->module && !make->install_lib.count && !make->install_dev.count)
  3607. {
  3608. if (make->importlib) strarray_add( &make->install_dev, make->importlib );
  3609. if (make->staticlib) strarray_add( &make->install_dev, make->staticlib );
  3610. else strarray_add( &make->install_lib, make->module );
  3611. }
  3612. make->include_paths = empty_strarray;
  3613. make->include_args = empty_strarray;
  3614. make->define_args = empty_strarray;
  3615. strarray_add( &make->define_args, "-D__WINESRC__" );
  3616. value = get_expanded_make_var_array( make, "EXTRAINCL" );
  3617. for (i = 0; i < value.count; i++)
  3618. if (!strncmp( value.str[i], "-I", 2 ))
  3619. strarray_add_uniq( &make->include_paths, value.str[i] + 2 );
  3620. else
  3621. strarray_add_uniq( &make->define_args, value.str[i] );
  3622. strarray_addall( &make->define_args, get_expanded_make_var_array( make, "EXTRADEFS" ));
  3623. strarray_add( &make->include_args, strmake( "-I%s", obj_dir_path( make, "" )));
  3624. if (make->src_dir)
  3625. strarray_add( &make->include_args, strmake( "-I%s", make->src_dir ));
  3626. if (make->parent_dir)
  3627. strarray_add( &make->include_args, strmake( "-I%s", src_dir_path( make, make->parent_dir )));
  3628. strarray_add( &make->include_args, "-Iinclude" );
  3629. if (root_src_dir) strarray_add( &make->include_args, strmake( "-I%s", root_src_dir_path( "include" )));
  3630. list_init( &make->sources );
  3631. list_init( &make->includes );
  3632. for (var = source_vars; *var; var++)
  3633. {
  3634. value = get_expanded_make_var_array( make, *var );
  3635. for (i = 0; i < value.count; i++) add_src_file( make, value.str[i] );
  3636. }
  3637. add_generated_sources( make );
  3638. if (!make->use_msvcrt && !has_object_file( make ))
  3639. {
  3640. strarray_add( &make->extradllflags, "-mno-cygwin" );
  3641. make->use_msvcrt = 1;
  3642. }
  3643. if (make->use_msvcrt)
  3644. {
  3645. const char *crt_dll = add_crt_import( make, &make->imports );
  3646. if (crt_dll && !strncmp( crt_dll, "ucrt", 4 )) strarray_add( &make->define_args, "-D_UCRT" );
  3647. else
  3648. {
  3649. unsigned int msvcrt_version = 0;
  3650. if (crt_dll) sscanf( crt_dll, "msvcr%u", &msvcrt_version );
  3651. strarray_add( &make->define_args, strmake( "-D_MSVCR_VER=%u", msvcrt_version ));
  3652. }
  3653. }
  3654. LIST_FOR_EACH_ENTRY( file, &make->includes, struct incl_file, entry ) parse_file( make, file, 0 );
  3655. LIST_FOR_EACH_ENTRY( file, &make->sources, struct incl_file, entry ) get_dependencies( file, file );
  3656. make->is_cross = crosstarget && make->use_msvcrt;
  3657. if (make->is_cross)
  3658. {
  3659. strarray_addall_uniq( &cross_import_libs, make->imports );
  3660. strarray_addall_uniq( &cross_import_libs, make->extra_imports );
  3661. if (make->is_win16) strarray_add_uniq( &cross_import_libs, "kernel" );
  3662. strarray_add_uniq( &cross_import_libs, "winecrt0" );
  3663. strarray_add_uniq( &cross_import_libs, "kernel32" );
  3664. strarray_add_uniq( &cross_import_libs, "ntdll" );
  3665. }
  3666. if (!*dll_ext || make->is_cross)
  3667. for (i = 0; i < make->delayimports.count; i++)
  3668. strarray_add_uniq( &delay_import_libs, get_base_name( make->delayimports.str[i] ));
  3669. }
  3670. /*******************************************************************
  3671. * parse_makeflags
  3672. */
  3673. static void parse_makeflags( const char *flags )
  3674. {
  3675. const char *p = flags;
  3676. char *var, *buffer = xmalloc( strlen(flags) + 1 );
  3677. while (*p)
  3678. {
  3679. while (isspace(*p)) p++;
  3680. var = buffer;
  3681. while (*p && !isspace(*p))
  3682. {
  3683. if (*p == '\\' && p[1]) p++;
  3684. *var++ = *p++;
  3685. }
  3686. *var = 0;
  3687. if (var > buffer) set_make_variable( &cmdline_vars, buffer );
  3688. }
  3689. }
  3690. /*******************************************************************
  3691. * parse_option
  3692. */
  3693. static int parse_option( const char *opt )
  3694. {
  3695. if (opt[0] != '-')
  3696. {
  3697. if (strchr( opt, '=' )) return set_make_variable( &cmdline_vars, opt );
  3698. return 0;
  3699. }
  3700. switch(opt[1])
  3701. {
  3702. case 'f':
  3703. if (opt[2]) output_makefile_name = opt + 2;
  3704. break;
  3705. case 'R':
  3706. relative_dir_mode = 1;
  3707. break;
  3708. default:
  3709. fprintf( stderr, "Unknown option '%s'\n%s", opt, Usage );
  3710. exit(1);
  3711. }
  3712. return 1;
  3713. }
  3714. /*******************************************************************
  3715. * main
  3716. */
  3717. int main( int argc, char *argv[] )
  3718. {
  3719. const char *makeflags = getenv( "MAKEFLAGS" );
  3720. int i, j;
  3721. if (makeflags) parse_makeflags( makeflags );
  3722. i = 1;
  3723. while (i < argc)
  3724. {
  3725. if (parse_option( argv[i] ))
  3726. {
  3727. for (j = i; j < argc; j++) argv[j] = argv[j+1];
  3728. argc--;
  3729. }
  3730. else i++;
  3731. }
  3732. if (relative_dir_mode)
  3733. {
  3734. char *relpath;
  3735. if (argc != 3)
  3736. {
  3737. fprintf( stderr, "Option -R needs two directories\n%s", Usage );
  3738. exit( 1 );
  3739. }
  3740. relpath = get_relative_path( argv[1], argv[2] );
  3741. printf( "%s\n", relpath ? relpath : "." );
  3742. exit( 0 );
  3743. }
  3744. if (argc > 1) fatal_error( "Directory arguments not supported in this mode\n" );
  3745. atexit( cleanup_files );
  3746. signal( SIGTERM, exit_on_signal );
  3747. signal( SIGINT, exit_on_signal );
  3748. #ifdef SIGHUP
  3749. signal( SIGHUP, exit_on_signal );
  3750. #endif
  3751. for (i = 0; i < HASH_SIZE; i++) list_init( &files[i] );
  3752. top_makefile = parse_makefile( NULL );
  3753. target_flags = get_expanded_make_var_array( top_makefile, "TARGETFLAGS" );
  3754. msvcrt_flags = get_expanded_make_var_array( top_makefile, "MSVCRTFLAGS" );
  3755. dll_flags = get_expanded_make_var_array( top_makefile, "DLLFLAGS" );
  3756. extra_cflags = get_expanded_make_var_array( top_makefile, "EXTRACFLAGS" );
  3757. extra_cross_cflags = get_expanded_make_var_array( top_makefile, "EXTRACROSSCFLAGS" );
  3758. cpp_flags = get_expanded_make_var_array( top_makefile, "CPPFLAGS" );
  3759. lddll_flags = get_expanded_make_var_array( top_makefile, "LDDLLFLAGS" );
  3760. libs = get_expanded_make_var_array( top_makefile, "LIBS" );
  3761. enable_tests = get_expanded_make_var_array( top_makefile, "ENABLE_TESTS" );
  3762. delay_load_flag = get_expanded_make_variable( top_makefile, "DELAYLOADFLAG" );
  3763. top_install_lib = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_LIB" );
  3764. top_install_dev = get_expanded_make_var_array( top_makefile, "TOP_INSTALL_DEV" );
  3765. root_src_dir = get_expanded_make_variable( top_makefile, "srcdir" );
  3766. tools_dir = get_expanded_make_variable( top_makefile, "TOOLSDIR" );
  3767. tools_ext = get_expanded_make_variable( top_makefile, "TOOLSEXT" );
  3768. exe_ext = get_expanded_make_variable( top_makefile, "EXEEXT" );
  3769. man_ext = get_expanded_make_variable( top_makefile, "api_manext" );
  3770. dll_ext = (exe_ext && !strcmp( exe_ext, ".exe" )) ? "" : ".so";
  3771. crosstarget = get_expanded_make_variable( top_makefile, "CROSSTARGET" );
  3772. crossdebug = get_expanded_make_variable( top_makefile, "CROSSDEBUG" );
  3773. fontforge = get_expanded_make_variable( top_makefile, "FONTFORGE" );
  3774. convert = get_expanded_make_variable( top_makefile, "CONVERT" );
  3775. flex = get_expanded_make_variable( top_makefile, "FLEX" );
  3776. bison = get_expanded_make_variable( top_makefile, "BISON" );
  3777. ar = get_expanded_make_variable( top_makefile, "AR" );
  3778. ranlib = get_expanded_make_variable( top_makefile, "RANLIB" );
  3779. rsvg = get_expanded_make_variable( top_makefile, "RSVG" );
  3780. icotool = get_expanded_make_variable( top_makefile, "ICOTOOL" );
  3781. dlltool = get_expanded_make_variable( top_makefile, "DLLTOOL" );
  3782. msgfmt = get_expanded_make_variable( top_makefile, "MSGFMT" );
  3783. sed_cmd = get_expanded_make_variable( top_makefile, "SED_CMD" );
  3784. ln_s = get_expanded_make_variable( top_makefile, "LN_S" );
  3785. if (root_src_dir && !strcmp( root_src_dir, "." )) root_src_dir = NULL;
  3786. if (tools_dir && !strcmp( tools_dir, "." )) tools_dir = NULL;
  3787. if (!exe_ext) exe_ext = "";
  3788. if (!tools_ext) tools_ext = "";
  3789. if (!man_ext) man_ext = "3w";
  3790. top_makefile->src_dir = root_src_dir;
  3791. subdirs = get_expanded_make_var_array( top_makefile, "SUBDIRS" );
  3792. disabled_dirs = get_expanded_make_var_array( top_makefile, "DISABLED_SUBDIRS" );
  3793. submakes = xmalloc( subdirs.count * sizeof(*submakes) );
  3794. for (i = 0; i < subdirs.count; i++) submakes[i] = parse_makefile( subdirs.str[i] );
  3795. load_sources( top_makefile );
  3796. for (i = 0; i < subdirs.count; i++) load_sources( submakes[i] );
  3797. output_dependencies( top_makefile );
  3798. for (i = 0; i < subdirs.count; i++) output_dependencies( submakes[i] );
  3799. return 0;
  3800. }