10AWG conductors should be fine for 240 volt, 40 amps, single phase, copper, at a length of 30 feet. Actually 12AWG is the minimum wire size with a 3% voltage drop. Ampacity limits this to 30 amps though for RUW, T or TW insulation on the cord. If the insulation on the cable was FEP, FEPB, THHN, or XHHW then 40 amps max would work for 12AWG - the limiting factor is the insulation material, not the conductor material (the insulation would begin melting before the copper conductor).
The 10AWG conductors will only drop the voltage 1%, which is not a problem as most line voltages fluctuate way more than that on a regular basis.
If you are still interested in an extension cord, I made one for a 240V mill/drill machine - went to Lowes and bought a leftover length of AWG 10.3 and the proper plugs for each end of the cord. Total cost $28. Properly assembled, this should serve you well.
If you are going to run it outside between two buildings, the underground conduit is good advice. If you place this in a conduit, then 10AWG would be the minimum as the above is for free-air (no conduit) installations. Also, most plastic-sheathed cables should not be exposed to sunlight unless explicitly stated otherwise.